Add GetAuthenticateInfo method
This commit is contained in:
parent
3e69df87f8
commit
3a7f6a7228
|
|
@ -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; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -20,18 +20,14 @@ namespace Microsoft.AspNetCore.Http.Authentication
|
||||||
|
|
||||||
public abstract IEnumerable<AuthenticationDescription> GetAuthenticationSchemes();
|
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 abstract Task AuthenticateAsync(AuthenticateContext context);
|
||||||
|
|
||||||
public virtual async Task<ClaimsPrincipal> AuthenticateAsync(string authenticationScheme)
|
public virtual async Task<ClaimsPrincipal> AuthenticateAsync(string authenticationScheme)
|
||||||
{
|
{
|
||||||
if (authenticationScheme == null)
|
return (await GetAuthenticateInfoAsync(authenticationScheme))?.Principal;
|
||||||
{
|
|
||||||
throw new ArgumentNullException(nameof(authenticationScheme));
|
|
||||||
}
|
|
||||||
|
|
||||||
var context = new AuthenticateContext(authenticationScheme);
|
|
||||||
await AuthenticateAsync(context);
|
|
||||||
return context.Principal;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual Task ChallengeAsync()
|
public virtual Task ChallengeAsync()
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,7 @@ namespace Microsoft.AspNetCore.Http.Authentication.Internal
|
||||||
return describeContext.Results.Select(description => new AuthenticationDescription(description));
|
return describeContext.Results.Select(description => new AuthenticationDescription(description));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Remove once callers have been switched to GetAuthenticateInfoAsync
|
||||||
public override async Task AuthenticateAsync(AuthenticateContext context)
|
public override async Task AuthenticateAsync(AuthenticateContext context)
|
||||||
{
|
{
|
||||||
if (context == null)
|
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)
|
public override async Task ChallengeAsync(string authenticationScheme, AuthenticationProperties properties, ChallengeBehavior behavior)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(authenticationScheme))
|
if (string.IsNullOrEmpty(authenticationScheme))
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue