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 00000000..41ffdfbb --- /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 0fa57892..56d9dbad 100644 --- a/src/Microsoft.AspNetCore.Http.Abstractions/Authentication/AuthenticationManager.cs +++ b/src/Microsoft.AspNetCore.Http.Abstractions/Authentication/AuthenticationManager.cs @@ -20,18 +20,14 @@ public abstract class AuthenticationManager 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 a9f79ba6..257ca2d7 100644 --- a/src/Microsoft.AspNetCore.Http/Authentication/DefaultAuthenticationManager.cs +++ b/src/Microsoft.AspNetCore.Http/Authentication/DefaultAuthenticationManager.cs @@ -50,6 +50,7 @@ public override IEnumerable GetAuthenticationSchemes( 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 @@ public override async Task AuthenticateAsync(AuthenticateContext context) } } + 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))