Skip to content

Commit 84bce91

Browse files
authored
allow auth with user (redis 6) (#1410)
1 parent 8349c04 commit 84bce91

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

src/StackExchange.Redis/ConfigurationOptions.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ internal const string
7676
HighPrioritySocketThreads = "highPriorityThreads",
7777
KeepAlive = "keepAlive",
7878
ClientName = "name",
79+
User = "user",
7980
Password = "password",
8081
PreserveAsyncOrder = "preserveAsyncOrder",
8182
Proxy = "proxy",
@@ -104,6 +105,7 @@ internal const string
104105
DefaultDatabase,
105106
HighPrioritySocketThreads,
106107
KeepAlive,
108+
User,
107109
Password,
108110
PreserveAsyncOrder,
109111
Proxy,
@@ -305,6 +307,11 @@ public int ConnectTimeout
305307
public int KeepAlive { get { return keepAlive.GetValueOrDefault(-1); } set { keepAlive = value; } }
306308
#pragma warning restore RCS1128 // Use coalesce expression.
307309

310+
/// <summary>
311+
/// The user to use to authenticate with the server.
312+
/// </summary>
313+
public string User { get; set; }
314+
308315
/// <summary>
309316
/// The password to use to authenticate with the server.
310317
/// </summary>
@@ -441,6 +448,7 @@ public ConfigurationOptions Clone()
441448
allowAdmin = allowAdmin,
442449
defaultVersion = defaultVersion,
443450
connectTimeout = connectTimeout,
451+
User = User,
444452
Password = Password,
445453
tieBreaker = tieBreaker,
446454
writeBuffer = writeBuffer,
@@ -505,6 +513,7 @@ public string ToString(bool includePassword)
505513
Append(sb, OptionKeys.AllowAdmin, allowAdmin);
506514
Append(sb, OptionKeys.Version, defaultVersion);
507515
Append(sb, OptionKeys.ConnectTimeout, connectTimeout);
516+
Append(sb, OptionKeys.User, User);
508517
Append(sb, OptionKeys.Password, (includePassword || string.IsNullOrEmpty(Password)) ? Password : "*****");
509518
Append(sb, OptionKeys.TieBreaker, tieBreaker);
510519
Append(sb, OptionKeys.WriteBuffer, writeBuffer);
@@ -597,9 +606,10 @@ private static void Append(StringBuilder sb, string prefix, object value)
597606

598607
private void Clear()
599608
{
600-
ClientName = ServiceName = Password = tieBreaker = sslHost = configChannel = null;
609+
ClientName = ServiceName = User =Password = tieBreaker = sslHost = configChannel = null;
601610
keepAlive = syncTimeout = asyncTimeout = connectTimeout = writeBuffer = connectRetry = configCheckSeconds = DefaultDatabase = null;
602611
allowAdmin = abortOnConnectFail = highPrioritySocketThreads = resolveDns = ssl = null;
612+
SslProtocols = null;
603613
defaultVersion = null;
604614
EndPoints.Clear();
605615
commandMap = null;
@@ -686,6 +696,9 @@ private void DoParse(string configuration, bool ignoreUnknown)
686696
case OptionKeys.Version:
687697
DefaultVersion = OptionKeys.ParseVersion(key, value);
688698
break;
699+
case OptionKeys.User:
700+
User = value;
701+
break;
689702
case OptionKeys.Password:
690703
Password = value;
691704
break;

src/StackExchange.Redis/ServerEndPoint.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -753,8 +753,16 @@ private async Task HandshakeAsync(PhysicalConnection connection, LogProxy log)
753753
return;
754754
}
755755
Message msg;
756-
string password = Multiplexer.RawConfig.Password;
757-
if (!string.IsNullOrWhiteSpace(password))
756+
// note that we need "" (not null) for password in the case of 'nopass' logins
757+
string user = Multiplexer.RawConfig.User, password = Multiplexer.RawConfig.Password ?? "";
758+
if (!string.IsNullOrWhiteSpace(user))
759+
{
760+
log?.WriteLine("Authenticating (user/password)");
761+
msg = Message.Create(-1, CommandFlags.FireAndForget, RedisCommand.AUTH, (RedisValue)user, (RedisValue)password);
762+
msg.SetInternalCall();
763+
await WriteDirectOrQueueFireAndForgetAsync(connection, msg, ResultProcessor.DemandOK).ForAwait();
764+
}
765+
else if (!string.IsNullOrWhiteSpace(password))
758766
{
759767
log?.WriteLine("Authenticating (password)");
760768
msg = Message.Create(-1, CommandFlags.FireAndForget, RedisCommand.AUTH, (RedisValue)password);

0 commit comments

Comments
 (0)