-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#1 Implement a full authentication handler.
- Loading branch information
Showing
9 changed files
with
347 additions
and
43 deletions.
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
src/Microsoft.AspNet.IISPlatformHandler/AuthenticationHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Linq; | ||
using System.Security.Claims; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNet.Http; | ||
using Microsoft.AspNet.Http.Features.Authentication; | ||
|
||
namespace Microsoft.AspNet.IISPlatformHandler | ||
{ | ||
internal class AuthenticationHandler : IAuthenticationHandler | ||
{ | ||
internal AuthenticationHandler(HttpContext httpContext, IISPlatformHandlerOptions options, ClaimsPrincipal user) | ||
{ | ||
HttpContext = httpContext; | ||
User = user; | ||
Options = options; | ||
} | ||
|
||
internal HttpContext HttpContext { get; } | ||
|
||
internal IISPlatformHandlerOptions Options { get; } | ||
|
||
internal ClaimsPrincipal User { get; } | ||
|
||
internal IAuthenticationHandler PriorHandler { get; set; } | ||
|
||
public Task AuthenticateAsync(AuthenticateContext context) | ||
{ | ||
if (ShouldHandleScheme(context.AuthenticationScheme)) | ||
{ | ||
if (User != null) | ||
{ | ||
context.Authenticated(User, properties: null, | ||
description: Options.AuthenticationDescriptions.Where(descrip => | ||
string.Equals(User.Identity.AuthenticationType, descrip.AuthenticationScheme, StringComparison.Ordinal)).FirstOrDefault()?.Items); | ||
} | ||
else | ||
{ | ||
context.NotAuthenticated(); | ||
} | ||
} | ||
|
||
if (PriorHandler != null) | ||
{ | ||
return PriorHandler.AuthenticateAsync(context); | ||
} | ||
return Task.FromResult(0); | ||
} | ||
|
||
public Task ChallengeAsync(ChallengeContext context) | ||
{ | ||
bool handled = false; | ||
if (ShouldHandleScheme(context.AuthenticationScheme)) | ||
{ | ||
switch (context.Behavior) | ||
{ | ||
case ChallengeBehavior.Automatic: | ||
// If there is a principal already, invoke the forbidden code path | ||
if (User == null) | ||
{ | ||
goto case ChallengeBehavior.Unauthorized; | ||
} | ||
else | ||
{ | ||
goto case ChallengeBehavior.Forbidden; | ||
} | ||
case ChallengeBehavior.Unauthorized: | ||
HttpContext.Response.StatusCode = 401; | ||
// We would normally set the www-authenticate header here, but IIS does that for us. | ||
break; | ||
case ChallengeBehavior.Forbidden: | ||
HttpContext.Response.StatusCode = 403; | ||
handled = true; // No other handlers need to consider this challenge. | ||
break; | ||
} | ||
context.Accept(); | ||
} | ||
|
||
if (!handled && PriorHandler != null) | ||
{ | ||
return PriorHandler.ChallengeAsync(context); | ||
} | ||
return Task.FromResult(0); | ||
} | ||
|
||
public void GetDescriptions(DescribeSchemesContext context) | ||
{ | ||
foreach (var description in Options.AuthenticationDescriptions) | ||
{ | ||
context.Accept(description.Items); | ||
} | ||
|
||
if (PriorHandler != null) | ||
{ | ||
PriorHandler.GetDescriptions(context); | ||
} | ||
} | ||
|
||
public Task SignInAsync(SignInContext context) | ||
{ | ||
// Not supported, fall through | ||
if (PriorHandler != null) | ||
{ | ||
return PriorHandler.SignInAsync(context); | ||
} | ||
return Task.FromResult(0); | ||
} | ||
|
||
public Task SignOutAsync(SignOutContext context) | ||
{ | ||
// Not supported, fall through | ||
if (PriorHandler != null) | ||
{ | ||
return PriorHandler.SignOutAsync(context); | ||
} | ||
return Task.FromResult(0); | ||
} | ||
|
||
private bool ShouldHandleScheme(string authenticationScheme) | ||
{ | ||
if (Options.AutomaticAuthentication && string.IsNullOrEmpty(authenticationScheme)) | ||
{ | ||
return true; | ||
} | ||
foreach (var description in Options.AuthenticationDescriptions) | ||
{ | ||
if (string.Equals(description.AuthenticationScheme, authenticationScheme, StringComparison.Ordinal)) | ||
{ | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Microsoft.AspNet.IISPlatformHandler/IISPlatformHandlerDefaults.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.AspNet.IISPlatformHandler | ||
{ | ||
public class IISPlatformHandlerDefaults | ||
{ | ||
public const string Negotiate = "Negotiate"; | ||
public const string Ntlm = "NTLM"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.