Skip to content

Commit 43b91da

Browse files
authored
Stop splitting the UPN credential into username and domain parts unless specified (#5663)
* Stop splitting the UPN credential into username and domain parts unless specified and add unit test.
1 parent 4e6e777 commit 43b91da

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/System.ServiceModel.Primitives/src/System/ServiceModel/Security/SecurityUtils.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ internal static partial class SecurityUtils
201201
public const string Principal = "Principal";
202202
private static IIdentity s_anonymousIdentity;
203203
private static X509SecurityTokenAuthenticator s_nonValidatingX509Authenticator;
204+
internal const string EnableLegacyUpnUsernameFixString = "Switch.System.ServiceModel.EnableLegacyUpnUsernameFix";
205+
internal static bool s_enableLegacyUpnUsernameFix = AppContext.TryGetSwitch(EnableLegacyUpnUsernameFixString, out bool enabled) && enabled;
204206

205207
internal static string GetSpnFromIdentity(EndpointIdentity identity, EndpointAddress target)
206208
{
@@ -932,6 +934,11 @@ public static SecurityBindingElement GetIssuerSecurityBindingElement(ServiceMode
932934
}
933935

934936
internal static void FixNetworkCredential(ref NetworkCredential credential)
937+
{
938+
FixNetworkCredential(ref credential, s_enableLegacyUpnUsernameFix);
939+
}
940+
941+
internal static void FixNetworkCredential(ref NetworkCredential credential, bool enableLegacyUpnUsernameFix)
935942
{
936943
if (credential == null)
937944
{
@@ -952,7 +959,7 @@ internal static void FixNetworkCredential(ref NetworkCredential credential)
952959
credential = new NetworkCredential(partsWithSlashDelimiter[1], credential.Password, partsWithSlashDelimiter[0]);
953960
}
954961
}
955-
else if (partsWithSlashDelimiter.Length == 1 && partsWithAtDelimiter.Length == 2)
962+
else if (enableLegacyUpnUsernameFix && partsWithSlashDelimiter.Length == 1 && partsWithAtDelimiter.Length == 2)
956963
{
957964
if (!string.IsNullOrEmpty(partsWithAtDelimiter[0]) && !string.IsNullOrEmpty(partsWithAtDelimiter[1]))
958965
{
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System;
6+
using System.Net;
7+
using System.Reflection;
8+
using System.ServiceModel.Security;
9+
using Infrastructure.Common;
10+
using Xunit;
11+
12+
public static class SecurityUtilsTest
13+
{
14+
[WcfFact]
15+
public static void FixNetworkCredential_AppContext_EnableLegacyUpnUsernameFix()
16+
{
17+
Type t = Assembly.GetAssembly(typeof(WindowsClientCredential))
18+
.GetType(typeof(WindowsClientCredential).Namespace + ".SecurityUtils");
19+
20+
MethodInfo method = t.GetMethod("FixNetworkCredential", BindingFlags.NonPublic | BindingFlags.Static,
21+
null, new[] { typeof(NetworkCredential).MakeByRefType() }, null);
22+
23+
FieldInfo f = t.GetField("s_enableLegacyUpnUsernameFix", BindingFlags.Static | BindingFlags.NonPublic);
24+
25+
//default
26+
var credential = new NetworkCredential("[email protected]", "password");
27+
var parameters = new object[] { credential };
28+
method.Invoke(null, parameters);
29+
credential = (NetworkCredential)parameters[0];
30+
Assert.NotNull(credential);
31+
Assert.Equal("[email protected]", credential.UserName);
32+
Assert.Equal("password", credential.Password);
33+
Assert.Equal(string.Empty, credential.Domain);
34+
35+
//switch on
36+
f.SetValue(t, true);
37+
credential = new NetworkCredential("[email protected]", "password");
38+
parameters = new object[] { credential };
39+
method.Invoke(null, parameters);
40+
credential = (NetworkCredential)parameters[0];
41+
Assert.NotNull(credential);
42+
Assert.Equal("user", credential.UserName);
43+
Assert.Equal("password", credential.Password);
44+
Assert.Equal("domain.com", credential.Domain);
45+
46+
//switch off
47+
f.SetValue(t, false);
48+
credential = new NetworkCredential("[email protected]", "password");
49+
parameters = new object[] { credential };
50+
method.Invoke(null, parameters);
51+
credential = (NetworkCredential)parameters[0];
52+
Assert.NotNull(credential);
53+
Assert.Equal("[email protected]", credential.UserName);
54+
Assert.Equal("password", credential.Password);
55+
Assert.Equal(string.Empty, credential.Domain);
56+
}
57+
}

0 commit comments

Comments
 (0)