From 3a7f6a7228fe10b14996b97eed83bcb72684088a Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Tue, 26 Apr 2016 14:09:48 -0700 Subject: [PATCH] Add GetAuthenticateInfo method --- .../Authentication/AuthenticateInfo.cs | 24 +++++++++++++++++ .../Authentication/AuthenticationManager.cs | 12 +++------ .../DefaultAuthenticationManager.cs | 27 +++++++++++++++++++ 3 files changed, 55 insertions(+), 8 deletions(-) create mode 100644 src/Microsoft.AspNetCore.Http.Abstractions/Authentication/AuthenticateInfo.cs diff --git a/src/Microsoft.AspNetCore.Http.Abstractions/Authentication/AuthenticateInfo.cs b/src/Microsoft.AspNetCore.Http.Abstractions/Authentication/AuthenticateInfo.cs new file mode 100644 index 0000000000..41ffdfbb81 --- /dev/null +++ b/src/Microsoft.AspNetCore.Http.Abstractions/Authentication/AuthenticateInfo.cs @@ -0,0 +1,24 @@ +// 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.Security.Claims; + +namespace Microsoft.AspNetCore.Http.Authentication +{ + /// + /// Used to store the results of an Authenticate call. + /// + public class AuthenticateInfo + { + /// + /// The . + /// + public ClaimsPrincipal Principal { get; set; } + + /// + /// The . + /// + public AuthenticationProperties Properties { get; set; } + } +} diff --git a/src/Microsoft.AspNetCore.Http.Abstractions/Authentication/AuthenticationManager.cs b/src/Microsoft.AspNetCore.Http.Abstractions/Authentication/AuthenticationManager.cs index 0fa5789218..56d9dbad5f 100644 --- a/src/Microsoft.AspNetCore.Http.Abstractions/Authentication/AuthenticationManager.cs +++ b/src/Microsoft.AspNetCore.Http.Abstractions/Authentication/AuthenticationManager.cs @@ -20,18 +20,14 @@ namespace Microsoft.AspNetCore.Http.Authentication public abstract IEnumerable GetAuthenticationSchemes(); + public abstract Task GetAuthenticateInfoAsync(string authenticationScheme); + + // Will remove once callees have been updated public abstract Task AuthenticateAsync(AuthenticateContext context); public virtual async Task AuthenticateAsync(string authenticationScheme) { - if (authenticationScheme == null) - { - throw new ArgumentNullException(nameof(authenticationScheme)); - } - - var context = new AuthenticateContext(authenticationScheme); - await AuthenticateAsync(context); - return context.Principal; + return (await GetAuthenticateInfoAsync(authenticationScheme))?.Principal; } public virtual Task ChallengeAsync() diff --git a/src/Microsoft.AspNetCore.Http/Authentication/DefaultAuthenticationManager.cs b/src/Microsoft.AspNetCore.Http/Authentication/DefaultAuthenticationManager.cs index a9f79ba630..257ca2d759 100644 --- a/src/Microsoft.AspNetCore.Http/Authentication/DefaultAuthenticationManager.cs +++ b/src/Microsoft.AspNetCore.Http/Authentication/DefaultAuthenticationManager.cs @@ -50,6 +50,7 @@ namespace Microsoft.AspNetCore.Http.Authentication.Internal return describeContext.Results.Select(description => new AuthenticationDescription(description)); } + // Remove once callers have been switched to GetAuthenticateInfoAsync public override async Task AuthenticateAsync(AuthenticateContext context) { if (context == null) @@ -69,6 +70,32 @@ namespace Microsoft.AspNetCore.Http.Authentication.Internal } } + public override async Task GetAuthenticateInfoAsync(string authenticationScheme) + { + if (authenticationScheme == null) + { + throw new ArgumentNullException(nameof(authenticationScheme)); + } + + var handler = HttpAuthenticationFeature.Handler; + var context = new AuthenticateContext(authenticationScheme); + if (handler != null) + { + await handler.AuthenticateAsync(context); + } + + if (!context.Accepted) + { + throw new InvalidOperationException($"No authentication handler is configured to authenticate for the scheme: {context.AuthenticationScheme}"); + } + + return new AuthenticateInfo + { + Principal = context.Principal, + Properties = new AuthenticationProperties(context.Properties) + }; + } + public override async Task ChallengeAsync(string authenticationScheme, AuthenticationProperties properties, ChallengeBehavior behavior) { if (string.IsNullOrEmpty(authenticationScheme))