|
| 1 | +--- |
| 2 | +categories: |
| 3 | +- docs |
| 4 | +- develop |
| 5 | +- stack |
| 6 | +- oss |
| 7 | +- rs |
| 8 | +- rc |
| 9 | +- oss |
| 10 | +- kubernetes |
| 11 | +- clients |
| 12 | +description: Connect your .NET application to a Redis database |
| 13 | +linkTitle: Connect |
| 14 | +title: Connect to the server |
| 15 | +weight: 2 |
| 16 | +--- |
| 17 | + |
| 18 | +## Basic connection |
| 19 | + |
| 20 | +You can connect to the server simply by passing a string of the |
| 21 | +form "hostname:port" to the `Connect()` method (for example, |
| 22 | +"localhost:6379"). However, you can also connect using a |
| 23 | +`ConfigurationOptions` parameter. Use this to specify a |
| 24 | +username, password, and many other options: |
| 25 | + |
| 26 | +```csharp |
| 27 | +using NRedisStack; |
| 28 | +using NRedisStack.RedisStackCommands; |
| 29 | +using StackExchange.Redis; |
| 30 | + |
| 31 | +ConfigurationOptions conf = new ConfigurationOptions { |
| 32 | + EndPoints = { "localhost:6379" }, |
| 33 | + User = "yourUsername", |
| 34 | + Password = "yourPassword" |
| 35 | +}; |
| 36 | + |
| 37 | +ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(conf); |
| 38 | +IDatabase db = redis.GetDatabase(); |
| 39 | + |
| 40 | +db.StringSet("foo", "bar"); |
| 41 | +Console.WriteLine(db.StringGet("foo")); // prints bar |
| 42 | +``` |
| 43 | + |
| 44 | +## Connect to a Redis cluster |
| 45 | + |
| 46 | +The basic connection will use the |
| 47 | +[Cluster API]({{< relref "/operate/rs/clusters/optimize/oss-cluster-api" >}}) |
| 48 | +if it is available without any special configuration. However, if you know |
| 49 | +the addresses and ports of several cluster nodes, you can specify them all |
| 50 | +during connection in the `Endpoints` parameter: |
| 51 | + |
| 52 | +```csharp |
| 53 | +ConfigurationOptions options = new ConfigurationOptions |
| 54 | +{ |
| 55 | + //list of available nodes of the cluster along with the endpoint port. |
| 56 | + EndPoints = { |
| 57 | + { "localhost", 16379 }, |
| 58 | + { "localhost", 16380 }, |
| 59 | + // ... |
| 60 | + }, |
| 61 | +}; |
| 62 | + |
| 63 | +ConnectionMultiplexer cluster = ConnectionMultiplexer.Connect(options); |
| 64 | +IDatabase db = cluster.GetDatabase(); |
| 65 | + |
| 66 | +db.StringSet("foo", "bar"); |
| 67 | +Console.WriteLine(db.StringGet("foo")); // prints bar |
| 68 | +``` |
| 69 | + |
| 70 | +## Connect to your production Redis with TLS |
| 71 | + |
| 72 | +When you deploy your application, use TLS and follow the [Redis security]({{< relref "/operate/oss_and_stack/management/security/" >}}) guidelines. |
| 73 | + |
| 74 | +Before connecting your application to the TLS-enabled Redis server, ensure that your certificates and private keys are in the correct format. |
| 75 | + |
| 76 | +To convert user certificate and private key from the PEM format to `pfx`, use this command: |
| 77 | + |
| 78 | +```bash |
| 79 | +openssl pkcs12 -inkey redis_user_private.key -in redis_user.crt -export -out redis.pfx |
| 80 | +``` |
| 81 | + |
| 82 | +Enter password to protect your `pfx` file. |
| 83 | + |
| 84 | +Establish a secure connection with your Redis database using this snippet. |
| 85 | + |
| 86 | +```csharp |
| 87 | +ConfigurationOptions options = new ConfigurationOptions |
| 88 | +{ |
| 89 | + EndPoints = { { "my-redis.cloud.redislabs.com", 6379 } }, |
| 90 | + User = "default", // use your Redis user. More info https://redis.io/docs/latest/operate/oss_and_stack/management/security/acl/ |
| 91 | + Password = "secret", // use your Redis password |
| 92 | + Ssl = true, |
| 93 | + SslProtocols = System.Security.Authentication.SslProtocols.Tls12 |
| 94 | +}; |
| 95 | + |
| 96 | +options.CertificateSelection += delegate |
| 97 | +{ |
| 98 | + return new X509Certificate2("redis.pfx", "secret"); // use the password you specified for pfx file |
| 99 | +}; |
| 100 | +options.CertificateValidation += ValidateServerCertificate; |
| 101 | + |
| 102 | +bool ValidateServerCertificate( |
| 103 | + object sender, |
| 104 | + X509Certificate? certificate, |
| 105 | + X509Chain? chain, |
| 106 | + SslPolicyErrors sslPolicyErrors) |
| 107 | +{ |
| 108 | + if (certificate == null) { |
| 109 | + return false; |
| 110 | + } |
| 111 | + |
| 112 | + var ca = new X509Certificate2("redis_ca.pem"); |
| 113 | + bool verdict = (certificate.Issuer == ca.Subject); |
| 114 | + if (verdict) { |
| 115 | + return true; |
| 116 | + } |
| 117 | + Console.WriteLine("Certificate error: {0}", sslPolicyErrors); |
| 118 | + return false; |
| 119 | +} |
| 120 | + |
| 121 | +ConnectionMultiplexer muxer = ConnectionMultiplexer.Connect(options); |
| 122 | + |
| 123 | +//Creation of the connection to the DB |
| 124 | +IDatabase conn = muxer.GetDatabase(); |
| 125 | + |
| 126 | +//send SET command |
| 127 | +conn.StringSet("foo", "bar"); |
| 128 | + |
| 129 | +//send GET command and print the value |
| 130 | +Console.WriteLine(conn.StringGet("foo")); |
| 131 | +``` |
| 132 | + |
| 133 | +## Multiplexing |
| 134 | + |
| 135 | +Although example code typically works with a single connection, |
| 136 | +real-world code often uses multiple connections at the same time. |
| 137 | +Opening and closing connections repeatedly is inefficient, so it is best |
| 138 | +to manage open connections carefully to avoid this. |
| 139 | + |
| 140 | +Several other |
| 141 | +Redis client libraries use *connection pools* to reuse a set of open |
| 142 | +connections efficiently. NRedisStack uses a different approach called |
| 143 | +*multiplexing*, which sends all client commands and responses over a |
| 144 | +single connection. NRedisStack manages multiplexing for you automatically. |
| 145 | +This gives high performance without requiring any extra coding. |
| 146 | +See |
| 147 | +[Connection pools and multiplexing]({{< relref "/develop/clients/pools-and-muxing" >}}) |
| 148 | +for more information. |
0 commit comments