X

Redis for .NET Developers – Redis Password

Redis

I wanted to cover some security aspect of Redis, using Redis Password feature. Redis uses config file to provide a password for it. The config file is usually located at /etc/redis/redis.conf if you are using linux. Look for the SECURITY section.

requirepass taswar-foobared

If you are using docker you can set the option when you start the docker container.

docker run --name my-redis -d redis redis-server --requirepass taswar-foobared

Afterwards if you are trying to connect to redis using redis-cli you will not be able to launch any commands in it,

# redis-cli
127.0.0.1:6379> set x hello
(error) NOAUTH Authentication required.

In order to set the value x, one will need to authenticate

127.0.0.1:6379> auth taswar-foobared
OK
127.0.0.1:6379> set x hello
OK
127.0.0.1:6379> get x
"hello"

If you are using StackExchange.Redis you can use the sample code below to connect, as you can see I am just adding the password to the connection string.

private static Lazy lazyConnection = new Lazy(() =>
{
    return ConnectionMultiplexer.Connect("localhostEndpoint,password=taswar-foobared");
});

public static ConnectionMultiplexer Connection
{
    get
    {
        return lazyConnection.Value;
    }
}

For other Redis topics

  1. Intro to Redis for .NET Developers
  2. Redis for .NET Developer – Connecting with C#
  3. Redis for .NET Developer – String Datatype
  4. Redis for .NET Developer – String Datatype part 2
  5. Redis for .NET Developer – Hash Datatype
  6. Redis for .NET Developer – Redis running in Docker
  7. Redis for .NET Developer – Redis running in Azure
  8. Redis for .NET Developer – Redis running in AWS ElastiCache
Categories: .NET Redis
Taswar Bhatti:
Related Post