Skip to content

Commit a7a1ea5

Browse files
committed
#8 re-enable and expand NTLM tests.
1 parent 67bd48f commit a7a1ea5

File tree

2 files changed

+80
-8
lines changed

2 files changed

+80
-8
lines changed

test/ServerComparison.FunctionalTests/NtlmAuthentationTest.cs

+41-4
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,8 @@ public class NtlmAuthenticationTests
1919
{
2020
[ConditionalTheory, Trait("ServerComparison.FunctionalTests", "ServerComparison.FunctionalTests")]
2121
[OSSkipCondition(OperatingSystems.Linux | OperatingSystems.MacOSX)]
22-
// TODO: Figure out why IISExpress failing
23-
//[InlineData(ServerType.IISExpress, RuntimeFlavor.CoreClr, RuntimeArchitecture.x86, "http://localhost:5050/")]
24-
//[InlineData(ServerType.IISExpress, RuntimeFlavor.Clr, RuntimeArchitecture.x64, "http://localhost:5051/")]
22+
[InlineData(ServerType.IISExpress, RuntimeFlavor.CoreClr, RuntimeArchitecture.x86, "http://localhost:5050/")]
23+
[InlineData(ServerType.IISExpress, RuntimeFlavor.Clr, RuntimeArchitecture.x64, "http://localhost:5051/")]
2524
[InlineData(ServerType.WebListener, RuntimeFlavor.Clr, RuntimeArchitecture.x86, "http://localhost:5052/")]
2625
[InlineData(ServerType.WebListener, RuntimeFlavor.CoreClr, RuntimeArchitecture.x64, "http://localhost:5052/")]
2726
public async Task NtlmAuthentication(ServerType serverType, RuntimeFlavor runtimeFlavor, RuntimeArchitecture architecture, string applicationBaseUrl)
@@ -61,14 +60,52 @@ public async Task NtlmAuthentication(ServerType serverType, RuntimeFlavor runtim
6160
Assert.Equal("Anonymous?True", responseText);
6261

6362
response = await httpClient.GetAsync("/Restricted");
63+
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
64+
Assert.Contains("NTLM", response.Headers.WwwAuthenticate.ToString());
65+
Assert.Contains("Negotiate", response.Headers.WwwAuthenticate.ToString());
6466

67+
response = await httpClient.GetAsync("/RestrictedNTLM");
6568
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
6669
Assert.Contains("NTLM", response.Headers.WwwAuthenticate.ToString());
70+
// Note IIS can't restrict a challenge to a specific auth type, the native auth modules always add themselves.
71+
// However WebListener can.
72+
if (serverType == ServerType.WebListener)
73+
{
74+
Assert.DoesNotContain("Negotiate", response.Headers.WwwAuthenticate.ToString());
75+
}
76+
else if (serverType == ServerType.IISExpress)
77+
{
78+
Assert.Contains("Negotiate", response.Headers.WwwAuthenticate.ToString());
79+
}
80+
81+
response = await httpClient.GetAsync("/Forbidden");
82+
Assert.Equal(HttpStatusCode.Forbidden, response.StatusCode);
6783

6884
httpClientHandler = new HttpClientHandler() { UseDefaultCredentials = true };
6985
httpClient = new HttpClient(httpClientHandler) { BaseAddress = new Uri(deploymentResult.ApplicationBaseUri) };
86+
87+
response = await httpClient.GetAsync("/AutoForbid");
88+
Assert.Equal(HttpStatusCode.Forbidden, response.StatusCode);
89+
7090
responseText = await httpClient.GetStringAsync("/Restricted");
71-
Assert.Equal("NotAnonymous", responseText);
91+
Assert.Equal("Negotiate", responseText);
92+
93+
responseText = await httpClient.GetStringAsync("/RestrictedNegotiate");
94+
Assert.Equal("Negotiate", responseText);
95+
96+
if (serverType == ServerType.WebListener)
97+
{
98+
responseText = await httpClient.GetStringAsync("/RestrictedNTLM");
99+
Assert.Equal("NTLM", responseText);
100+
}
101+
else if (serverType == ServerType.IISExpress)
102+
{
103+
response = await httpClient.GetAsync("/RestrictedNTLM");
104+
// This isn't a Forbidden because we authenticate with Negotiate and challenge for NTLM.
105+
Assert.Equal(HttpStatusCode.Unauthorized, response.StatusCode);
106+
// Note IIS can't restrict a challenge to a specific auth type, the native auth modules always add themselves,
107+
// so both Negotiate and NTLM get sent again.
108+
}
72109
}
73110
catch (XunitException)
74111
{

test/ServerComparison.TestSites/StartupNtlmAuthentication.cs

+39-4
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,63 @@ public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
5353
if ((app.Server as ServerInformation) != null)
5454
{
5555
var serverInformation = (ServerInformation)app.Server;
56-
serverInformation.Listener.AuthenticationManager.AuthenticationSchemes = AuthenticationSchemes.NTLM | AuthenticationSchemes.AllowAnonymous;
56+
serverInformation.Listener.AuthenticationManager.AuthenticationSchemes =
57+
AuthenticationSchemes.Negotiate | AuthenticationSchemes.NTLM | AuthenticationSchemes.AllowAnonymous;
5758
}
5859

5960
app.Use((context, next) =>
6061
{
61-
if (context.Request.Path.Equals(new PathString("/Anonymous")))
62+
if (context.Request.Path.Equals("/Anonymous"))
6263
{
6364
return context.Response.WriteAsync("Anonymous?" + !context.User.Identity.IsAuthenticated);
6465
}
6566

66-
if (context.Request.Path.Equals(new PathString("/Restricted")))
67+
if (context.Request.Path.Equals("/Restricted"))
6768
{
6869
if (context.User.Identity.IsAuthenticated)
6970
{
70-
return context.Response.WriteAsync("NotAnonymous");
71+
return context.Response.WriteAsync(context.User.Identity.AuthenticationType);
7172
}
7273
else
7374
{
7475
return context.Authentication.ChallengeAsync();
7576
}
7677
}
7778

79+
if (context.Request.Path.Equals("/Forbidden"))
80+
{
81+
return context.Authentication.ForbidAsync(string.Empty);
82+
}
83+
84+
if (context.Request.Path.Equals("/AutoForbid"))
85+
{
86+
return context.Authentication.ChallengeAsync();
87+
}
88+
89+
if (context.Request.Path.Equals("/RestrictedNegotiate"))
90+
{
91+
if (string.Equals("Negotiate", context.User.Identity.AuthenticationType, System.StringComparison.Ordinal))
92+
{
93+
return context.Response.WriteAsync("Negotiate");
94+
}
95+
else
96+
{
97+
return context.Authentication.ChallengeAsync("Negotiate");
98+
}
99+
}
100+
101+
if (context.Request.Path.Equals("/RestrictedNTLM"))
102+
{
103+
if (string.Equals("NTLM", context.User.Identity.AuthenticationType, System.StringComparison.Ordinal))
104+
{
105+
return context.Response.WriteAsync("NTLM");
106+
}
107+
else
108+
{
109+
return context.Authentication.ChallengeAsync("NTLM");
110+
}
111+
}
112+
78113
return context.Response.WriteAsync("Hello World");
79114
});
80115
}

0 commit comments

Comments
 (0)