Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit 3a7f6a7

Browse files
committed
Add GetAuthenticateInfo method
1 parent 3e69df8 commit 3a7f6a7

File tree

3 files changed

+55
-8
lines changed

3 files changed

+55
-8
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Security.Claims;
6+
7+
namespace Microsoft.AspNetCore.Http.Authentication
8+
{
9+
/// <summary>
10+
/// Used to store the results of an Authenticate call.
11+
/// </summary>
12+
public class AuthenticateInfo
13+
{
14+
/// <summary>
15+
/// The <see cref="ClaimsPrincipal"/>.
16+
/// </summary>
17+
public ClaimsPrincipal Principal { get; set; }
18+
19+
/// <summary>
20+
/// The <see cref="AuthenticationProperties"/>.
21+
/// </summary>
22+
public AuthenticationProperties Properties { get; set; }
23+
}
24+
}

src/Microsoft.AspNetCore.Http.Abstractions/Authentication/AuthenticationManager.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,14 @@ public abstract class AuthenticationManager
2020

2121
public abstract IEnumerable<AuthenticationDescription> GetAuthenticationSchemes();
2222

23+
public abstract Task<AuthenticateInfo> GetAuthenticateInfoAsync(string authenticationScheme);
24+
25+
// Will remove once callees have been updated
2326
public abstract Task AuthenticateAsync(AuthenticateContext context);
2427

2528
public virtual async Task<ClaimsPrincipal> AuthenticateAsync(string authenticationScheme)
2629
{
27-
if (authenticationScheme == null)
28-
{
29-
throw new ArgumentNullException(nameof(authenticationScheme));
30-
}
31-
32-
var context = new AuthenticateContext(authenticationScheme);
33-
await AuthenticateAsync(context);
34-
return context.Principal;
30+
return (await GetAuthenticateInfoAsync(authenticationScheme))?.Principal;
3531
}
3632

3733
public virtual Task ChallengeAsync()

src/Microsoft.AspNetCore.Http/Authentication/DefaultAuthenticationManager.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public override IEnumerable<AuthenticationDescription> GetAuthenticationSchemes(
5050
return describeContext.Results.Select(description => new AuthenticationDescription(description));
5151
}
5252

53+
// Remove once callers have been switched to GetAuthenticateInfoAsync
5354
public override async Task AuthenticateAsync(AuthenticateContext context)
5455
{
5556
if (context == null)
@@ -69,6 +70,32 @@ public override async Task AuthenticateAsync(AuthenticateContext context)
6970
}
7071
}
7172

73+
public override async Task<AuthenticateInfo> GetAuthenticateInfoAsync(string authenticationScheme)
74+
{
75+
if (authenticationScheme == null)
76+
{
77+
throw new ArgumentNullException(nameof(authenticationScheme));
78+
}
79+
80+
var handler = HttpAuthenticationFeature.Handler;
81+
var context = new AuthenticateContext(authenticationScheme);
82+
if (handler != null)
83+
{
84+
await handler.AuthenticateAsync(context);
85+
}
86+
87+
if (!context.Accepted)
88+
{
89+
throw new InvalidOperationException($"No authentication handler is configured to authenticate for the scheme: {context.AuthenticationScheme}");
90+
}
91+
92+
return new AuthenticateInfo
93+
{
94+
Principal = context.Principal,
95+
Properties = new AuthenticationProperties(context.Properties)
96+
};
97+
}
98+
7299
public override async Task ChallengeAsync(string authenticationScheme, AuthenticationProperties properties, ChallengeBehavior behavior)
73100
{
74101
if (string.IsNullOrEmpty(authenticationScheme))

0 commit comments

Comments
 (0)