Add GetAuthenticateInfo method

This commit is contained in:
Hao Kung 2016-04-26 14:09:48 -07:00
parent 3e69df87f8
commit 3a7f6a7228
3 changed files with 55 additions and 8 deletions

View File

@ -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
{
/// <summary>
/// Used to store the results of an Authenticate call.
/// </summary>
public class AuthenticateInfo
{
/// <summary>
/// The <see cref="ClaimsPrincipal"/>.
/// </summary>
public ClaimsPrincipal Principal { get; set; }
/// <summary>
/// The <see cref="AuthenticationProperties"/>.
/// </summary>
public AuthenticationProperties Properties { get; set; }
}
}

View File

@ -20,18 +20,14 @@ namespace Microsoft.AspNetCore.Http.Authentication
public abstract IEnumerable<AuthenticationDescription> GetAuthenticationSchemes();
public abstract Task<AuthenticateInfo> GetAuthenticateInfoAsync(string authenticationScheme);
// Will remove once callees have been updated
public abstract Task AuthenticateAsync(AuthenticateContext context);
public virtual async Task<ClaimsPrincipal> 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()

View File

@ -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<AuthenticateInfo> 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))