Add an option to disable windown auth

This commit is contained in:
Pavel Krymets 2015-11-23 15:37:46 -08:00
parent 8ee803d255
commit cc384ff272
3 changed files with 71 additions and 12 deletions

View File

@ -49,20 +49,24 @@ namespace Microsoft.AspNet.IISPlatformHandler
UpdateScheme(httpContext);
UpdateRemoteIp(httpContext);
var winPrincipal = UpdateUser(httpContext);
var handler = new AuthenticationHandler(httpContext, _options, winPrincipal);
AttachAuthenticationHandler(handler);
try
if (_options.FlowWindowsAuthentication)
{
var winPrincipal = UpdateUser(httpContext);
var handler = new AuthenticationHandler(httpContext, _options, winPrincipal);
AttachAuthenticationHandler(handler);
try
{
await _next(httpContext);
}
finally
{
DetachAuthenticationhandler(handler);
}
}
else
{
await _next(httpContext);
}
finally
{
DetachAuthenticationhandler(handler);
}
}
private static void UpdateScheme(HttpContext httpContext)

View File

@ -15,6 +15,12 @@ namespace Microsoft.AspNet.IISPlatformHandler
/// </summary>
public bool AutomaticAuthentication { get; set; } = true;
/// <summary>
/// If true authentication middleware will try to authenticate using platform handler windows authentication
/// If false authentication middleware won't be added
/// </summary>
public bool FlowWindowsAuthentication { get; set; } = true;
/// <summary>
/// Additional information about the authentication type which is made available to the application.
/// </summary>

View File

@ -5,6 +5,9 @@ using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http.Features.Authentication;
using Microsoft.AspNet.Http.Internal;
using Microsoft.AspNet.IISPlatformHandler;
using Microsoft.AspNet.TestHost;
using Xunit;
@ -108,5 +111,51 @@ namespace Microsoft.AspNet.IISPlatformHandler
await server.CreateClient().SendAsync(req);
Assert.True(assertsExecuted);
}
[Fact]
public async Task AddsAuthenticationHandlerByDefault()
{
var assertsExecuted = false;
var server = TestServer.Create(app =>
{
app.UseIISPlatformHandler();
app.Run(context =>
{
var auth = (IHttpAuthenticationFeature)context.Features[typeof(IHttpAuthenticationFeature)];
Assert.NotNull(auth);
Assert.IsAssignableFrom<AuthenticationHandler>(auth.Handler);
assertsExecuted = true;
return Task.FromResult(0);
});
});
var req = new HttpRequestMessage(HttpMethod.Get, "");
await server.CreateClient().SendAsync(req);
Assert.True(assertsExecuted);
}
[Fact]
public async Task DoesNotAddAuthenticationHandlerIfWindowsAuthDisabled()
{
var assertsExecuted = false;
var server = TestServer.Create(app =>
{
app.UseIISPlatformHandler(options => options.FlowWindowsAuthentication = false);
app.Run(context =>
{
var auth = (IHttpAuthenticationFeature)context.Features[typeof(IHttpAuthenticationFeature)];
Assert.Null(auth);
assertsExecuted = true;
return Task.FromResult(0);
});
});
var req = new HttpRequestMessage(HttpMethod.Get, "");
await server.CreateClient().SendAsync(req);
Assert.True(assertsExecuted);
}
}
}
}