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

Commit 13925be

Browse files
committed
Initial Auth 2.0 checkin
1 parent a0bb843 commit 13925be

34 files changed

+2070
-0
lines changed

HttpAbstractions.sln

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{ED7BCAC5
5757
build\Key.snk = build\Key.snk
5858
EndProjectSection
5959
EndProject
60+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Authentication.Abstractions", "src\Microsoft.AspNetCore.Authentication.Abstractions\Microsoft.AspNetCore.Authentication.Abstractions.csproj", "{3D8C9A87-5DFB-4EC0-9CB6-174AD3B33852}"
61+
EndProject
62+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Authentication.Core", "src\Microsoft.AspNetCore.Authentication.Core\Microsoft.AspNetCore.Authentication.Core.csproj", "{73CA3145-91BD-4DA5-BC74-40008DE7EA98}"
63+
EndProject
64+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AspNetCore.Authentication.Core.Test", "test\Microsoft.AspNetCore.Authentication.Core.Test\Microsoft.AspNetCore.Authentication.Core.Test.csproj", "{A85950C5-2794-47E2-8EAA-05A1DC7C6DA7}"
65+
EndProject
6066
Global
6167
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6268
Debug|Any CPU = Debug|Any CPU
@@ -223,6 +229,42 @@ Global
223229
{1D0764B4-1DEB-4232-A714-D4B7E846918A}.Release|Mixed Platforms.Build.0 = Release|Any CPU
224230
{1D0764B4-1DEB-4232-A714-D4B7E846918A}.Release|x86.ActiveCfg = Release|Any CPU
225231
{1D0764B4-1DEB-4232-A714-D4B7E846918A}.Release|x86.Build.0 = Release|Any CPU
232+
{3D8C9A87-5DFB-4EC0-9CB6-174AD3B33852}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
233+
{3D8C9A87-5DFB-4EC0-9CB6-174AD3B33852}.Debug|Any CPU.Build.0 = Debug|Any CPU
234+
{3D8C9A87-5DFB-4EC0-9CB6-174AD3B33852}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
235+
{3D8C9A87-5DFB-4EC0-9CB6-174AD3B33852}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
236+
{3D8C9A87-5DFB-4EC0-9CB6-174AD3B33852}.Debug|x86.ActiveCfg = Debug|Any CPU
237+
{3D8C9A87-5DFB-4EC0-9CB6-174AD3B33852}.Debug|x86.Build.0 = Debug|Any CPU
238+
{3D8C9A87-5DFB-4EC0-9CB6-174AD3B33852}.Release|Any CPU.ActiveCfg = Release|Any CPU
239+
{3D8C9A87-5DFB-4EC0-9CB6-174AD3B33852}.Release|Any CPU.Build.0 = Release|Any CPU
240+
{3D8C9A87-5DFB-4EC0-9CB6-174AD3B33852}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
241+
{3D8C9A87-5DFB-4EC0-9CB6-174AD3B33852}.Release|Mixed Platforms.Build.0 = Release|Any CPU
242+
{3D8C9A87-5DFB-4EC0-9CB6-174AD3B33852}.Release|x86.ActiveCfg = Release|Any CPU
243+
{3D8C9A87-5DFB-4EC0-9CB6-174AD3B33852}.Release|x86.Build.0 = Release|Any CPU
244+
{73CA3145-91BD-4DA5-BC74-40008DE7EA98}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
245+
{73CA3145-91BD-4DA5-BC74-40008DE7EA98}.Debug|Any CPU.Build.0 = Debug|Any CPU
246+
{73CA3145-91BD-4DA5-BC74-40008DE7EA98}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
247+
{73CA3145-91BD-4DA5-BC74-40008DE7EA98}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
248+
{73CA3145-91BD-4DA5-BC74-40008DE7EA98}.Debug|x86.ActiveCfg = Debug|Any CPU
249+
{73CA3145-91BD-4DA5-BC74-40008DE7EA98}.Debug|x86.Build.0 = Debug|Any CPU
250+
{73CA3145-91BD-4DA5-BC74-40008DE7EA98}.Release|Any CPU.ActiveCfg = Release|Any CPU
251+
{73CA3145-91BD-4DA5-BC74-40008DE7EA98}.Release|Any CPU.Build.0 = Release|Any CPU
252+
{73CA3145-91BD-4DA5-BC74-40008DE7EA98}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
253+
{73CA3145-91BD-4DA5-BC74-40008DE7EA98}.Release|Mixed Platforms.Build.0 = Release|Any CPU
254+
{73CA3145-91BD-4DA5-BC74-40008DE7EA98}.Release|x86.ActiveCfg = Release|Any CPU
255+
{73CA3145-91BD-4DA5-BC74-40008DE7EA98}.Release|x86.Build.0 = Release|Any CPU
256+
{A85950C5-2794-47E2-8EAA-05A1DC7C6DA7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
257+
{A85950C5-2794-47E2-8EAA-05A1DC7C6DA7}.Debug|Any CPU.Build.0 = Debug|Any CPU
258+
{A85950C5-2794-47E2-8EAA-05A1DC7C6DA7}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
259+
{A85950C5-2794-47E2-8EAA-05A1DC7C6DA7}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
260+
{A85950C5-2794-47E2-8EAA-05A1DC7C6DA7}.Debug|x86.ActiveCfg = Debug|Any CPU
261+
{A85950C5-2794-47E2-8EAA-05A1DC7C6DA7}.Debug|x86.Build.0 = Debug|Any CPU
262+
{A85950C5-2794-47E2-8EAA-05A1DC7C6DA7}.Release|Any CPU.ActiveCfg = Release|Any CPU
263+
{A85950C5-2794-47E2-8EAA-05A1DC7C6DA7}.Release|Any CPU.Build.0 = Release|Any CPU
264+
{A85950C5-2794-47E2-8EAA-05A1DC7C6DA7}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
265+
{A85950C5-2794-47E2-8EAA-05A1DC7C6DA7}.Release|Mixed Platforms.Build.0 = Release|Any CPU
266+
{A85950C5-2794-47E2-8EAA-05A1DC7C6DA7}.Release|x86.ActiveCfg = Release|Any CPU
267+
{A85950C5-2794-47E2-8EAA-05A1DC7C6DA7}.Release|x86.Build.0 = Release|Any CPU
226268
EndGlobalSection
227269
GlobalSection(SolutionProperties) = preSolution
228270
HideSolutionNode = FALSE
@@ -244,5 +286,8 @@ Global
244286
{E6BB7AD1-BD10-4A23-B780-F4A86ADF00D1} = {F31FF137-390C-49BF-A3BD-7C6ED3597C21}
245287
{1D0764B4-1DEB-4232-A714-D4B7E846918A} = {982F09D8-621E-4872-BA7B-BBDEA47D1EFD}
246288
{ED7BCAC5-2796-44BD-9954-7C248263BC8B} = {C6C48D5F-B289-4150-A6FC-77A5C7064BCE}
289+
{3D8C9A87-5DFB-4EC0-9CB6-174AD3B33852} = {A5A15F1C-885A-452A-A731-B0173DDBD913}
290+
{73CA3145-91BD-4DA5-BC74-40008DE7EA98} = {A5A15F1C-885A-452A-A731-B0173DDBD913}
291+
{A85950C5-2794-47E2-8EAA-05A1DC7C6DA7} = {F31FF137-390C-49BF-A3BD-7C6ED3597C21}
247292
EndGlobalSection
248293
EndGlobal
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 Microsoft.AspNetCore.Http;
5+
6+
namespace Microsoft.AspNetCore.Authentication
7+
{
8+
/// <summary>
9+
/// Base class used by <see cref="IAuthenticationHandler"/> methods.
10+
/// </summary>
11+
public class AuthenticateContext : BaseAuthenticationContext
12+
{
13+
/// <summary>
14+
/// Constructor.
15+
/// </summary>
16+
/// <param name="context">The <see cref="HttpContext"/> context.</param>
17+
/// <param name="authenticationScheme">The name of the authentication scheme.</param>
18+
public AuthenticateContext(HttpContext context, string authenticationScheme) : base(context, authenticationScheme, properties: null)
19+
{ }
20+
}
21+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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.Authentication
8+
{
9+
/// <summary>
10+
/// Contains the result of an Authenticate call
11+
/// </summary>
12+
public class AuthenticateResult
13+
{
14+
private AuthenticateResult() { }
15+
16+
/// <summary>
17+
/// If a ticket was produced, authenticate was successful.
18+
/// </summary>
19+
public bool Succeeded => Ticket != null;
20+
21+
/// <summary>
22+
/// The authentication ticket.
23+
/// </summary>
24+
public AuthenticationTicket Ticket { get; private set; }
25+
26+
/// <summary>
27+
/// Gets the claims-principal with authenticated user identities.
28+
/// </summary>
29+
public ClaimsPrincipal Principal => Ticket?.Principal;
30+
31+
/// <summary>
32+
/// Additional state values for the authentication session.
33+
/// </summary>
34+
public AuthenticationProperties Properties => Ticket?.Properties;
35+
36+
/// <summary>
37+
/// Holds failure information from the authentication.
38+
/// </summary>
39+
public Exception Failure { get; private set; }
40+
41+
/// <summary>
42+
/// Indicates that stage of authentication was directly handled by user intervention and no
43+
/// further processing should be attempted.
44+
/// </summary>
45+
public bool Handled { get; private set; }
46+
47+
/// <summary>
48+
/// Indicates that there was no information returned for this authentication scheme.
49+
/// </summary>
50+
public bool Nothing { get; private set; }
51+
52+
/// <summary>
53+
/// Indicates that authentication was successful.
54+
/// </summary>
55+
/// <param name="ticket">The ticket representing the authentication result.</param>
56+
/// <returns>The result.</returns>
57+
public static AuthenticateResult Success(AuthenticationTicket ticket)
58+
{
59+
if (ticket == null)
60+
{
61+
throw new ArgumentNullException(nameof(ticket));
62+
}
63+
return new AuthenticateResult() { Ticket = ticket };
64+
}
65+
66+
/// <summary>
67+
/// Indicates that stage of authentication was directly handled by user intervention and no
68+
/// further processing should be attempted.
69+
/// </summary>
70+
/// <returns>The result.</returns>
71+
public static AuthenticateResult Handle()
72+
{
73+
return new AuthenticateResult() { Handled = true };
74+
}
75+
76+
/// <summary>
77+
/// Indicates that there was no information returned for this authentication scheme.
78+
/// </summary>
79+
/// <returns>The result.</returns>
80+
public static AuthenticateResult None()
81+
{
82+
return new AuthenticateResult() { Nothing = true };
83+
}
84+
85+
/// <summary>
86+
/// Indicates that there was a failure during authentication.
87+
/// </summary>
88+
/// <param name="failure">The failure exception.</param>
89+
/// <returns>The result.</returns>
90+
public static AuthenticateResult Fail(Exception failure)
91+
{
92+
return new AuthenticateResult() { Failure = failure };
93+
}
94+
95+
/// <summary>
96+
/// Indicates that there was a failure during authentication.
97+
/// </summary>
98+
/// <param name="failureMessage">The failure message.</param>
99+
/// <returns>The result.</returns>
100+
public static AuthenticateResult Fail(string failureMessage)
101+
{
102+
return new AuthenticateResult() { Failure = new Exception(failureMessage) };
103+
}
104+
}
105+
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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.Security.Claims;
5+
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Http;
7+
using Microsoft.Extensions.DependencyInjection;
8+
9+
namespace Microsoft.AspNetCore.Authentication
10+
{
11+
/// <summary>
12+
/// Extension methods to expose Authentication on HttpContext.
13+
/// </summary>
14+
public static class AuthenticationHttpContextExtensions
15+
{
16+
/// <summary>
17+
/// Extension method for authenticate using the <see cref="AuthenticationOptions.DefaultAuthenticationScheme"/> scheme.
18+
/// </summary>
19+
/// <param name="context">The <see cref="HttpContext"/> context.</param>
20+
/// <returns>The <see cref="AuthenticateResult"/>.</returns>
21+
public static Task<AuthenticateResult> AuthenticateAsync(this HttpContext context) =>
22+
context.AuthenticateAsync(scheme: null);
23+
24+
/// <summary>
25+
/// Extension method for authenticate.
26+
/// </summary>
27+
/// <param name="context">The <see cref="HttpContext"/> context.</param>
28+
/// <param name="scheme">The name of the authentication scheme.</param>
29+
/// <returns>The <see cref="AuthenticateResult"/>.</returns>
30+
public static Task<AuthenticateResult> AuthenticateAsync(this HttpContext context, string scheme) =>
31+
context.RequestServices.GetRequiredService<IAuthenticationService>().AuthenticateAsync(context, scheme);
32+
33+
/// <summary>
34+
/// Extension method for Challenge.
35+
/// </summary>
36+
/// <param name="context">The <see cref="HttpContext"/> context.</param>
37+
/// <param name="scheme">The name of the authentication scheme.</param>
38+
/// <returns>The result.</returns>
39+
public static Task ChallengeAsync(this HttpContext context, string scheme) =>
40+
context.ChallengeAsync(scheme, properties: null);
41+
42+
/// <summary>
43+
/// Extension method for authenticate using the <see cref="AuthenticationOptions.DefaultChallengeScheme"/> scheme.
44+
/// </summary>
45+
/// <param name="context">The <see cref="HttpContext"/> context.</param>
46+
/// <returns>The task.</returns>
47+
public static Task ChallengeAsync(this HttpContext context) =>
48+
context.ChallengeAsync(scheme: null, properties: null);
49+
50+
/// <summary>
51+
/// Extension method for Challenge.
52+
/// </summary>
53+
/// <param name="context">The <see cref="HttpContext"/> context.</param>
54+
/// <param name="scheme">The name of the authentication scheme.</param>
55+
/// <param name="properties">The <see cref="AuthenticationProperties"/> properties.</param>
56+
/// <returns>The task.</returns>
57+
public static Task ChallengeAsync(this HttpContext context, string scheme, AuthenticationProperties properties) =>
58+
context.ChallengeAsync(scheme, properties: properties, behavior: ChallengeBehavior.Automatic);
59+
60+
/// <summary>
61+
/// Extension method for Challenge.
62+
/// </summary>
63+
/// <param name="context">The <see cref="HttpContext"/> context.</param>
64+
/// <param name="scheme">The name of the authentication scheme.</param>
65+
/// <param name="properties">The <see cref="AuthenticationProperties"/> properties.</param>
66+
/// <param name="behavior">The <see cref="ChallengeBehavior"/> behavior.</param>
67+
/// <returns>The task.</returns>
68+
public static Task ChallengeAsync(this HttpContext context, string scheme, AuthenticationProperties properties, ChallengeBehavior behavior) =>
69+
context.RequestServices.GetRequiredService<IAuthenticationService>().ChallengeAsync(context, scheme, properties, behavior);
70+
71+
/// <summary>
72+
/// Extension method for Forbid.
73+
/// </summary>
74+
/// <param name="context">The <see cref="HttpContext"/> context.</param>
75+
/// <param name="scheme">The name of the authentication scheme.</param>
76+
/// <returns>The task.</returns>
77+
public static Task ForbidAsync(this HttpContext context, string scheme) =>
78+
context.ForbidAsync(scheme, properties: null);
79+
80+
/// <summary>
81+
/// Extension method for Forbid.
82+
/// </summary>
83+
/// <param name="context">The <see cref="HttpContext"/> context.</param>
84+
/// <param name="scheme">The name of the authentication scheme.</param>
85+
/// <param name="properties">The <see cref="AuthenticationProperties"/> properties.</param>
86+
/// <returns>The task.</returns>
87+
public static Task ForbidAsync(this HttpContext context, string scheme, AuthenticationProperties properties) =>
88+
context.RequestServices.GetRequiredService<IAuthenticationService>().ChallengeAsync(context, scheme, properties, ChallengeBehavior.Forbidden);
89+
90+
/// <summary>
91+
/// Extension method for SignIn.
92+
/// </summary>
93+
/// <param name="context">The <see cref="HttpContext"/> context.</param>
94+
/// <param name="scheme">The name of the authentication scheme.</param>
95+
/// <param name="principal">The user.</param>
96+
/// <returns>The task.</returns>
97+
public static Task SignInAsync(this HttpContext context, string scheme, ClaimsPrincipal principal) =>
98+
context.SignInAsync(scheme, principal, properties: null);
99+
100+
/// <summary>
101+
/// Extension method for SignIn using the <see cref="AuthenticationOptions.DefaultSignInScheme"/>.
102+
/// </summary>
103+
/// <param name="context">The <see cref="HttpContext"/> context.</param>
104+
/// <param name="principal">The user.</param>
105+
/// <returns>The task.</returns>
106+
public static Task SignInAsync(this HttpContext context, ClaimsPrincipal principal) =>
107+
context.SignInAsync(scheme: null, principal: principal, properties: null);
108+
109+
/// <summary>
110+
/// Extension method for SignIn using the <see cref="AuthenticationOptions.DefaultSignInScheme"/>.
111+
/// </summary>
112+
/// <param name="context">The <see cref="HttpContext"/> context.</param>
113+
/// <param name="principal">The user.</param>
114+
/// <param name="properties">The <see cref="AuthenticationProperties"/> properties.</param>
115+
/// <returns>The task.</returns>
116+
public static Task SignInAsync(this HttpContext context, ClaimsPrincipal principal, AuthenticationProperties properties) =>
117+
context.SignInAsync(scheme: null, principal: principal, properties: properties);
118+
119+
/// <summary>
120+
/// Extension method for SignIn.
121+
/// </summary>
122+
/// <param name="context">The <see cref="HttpContext"/> context.</param>
123+
/// <param name="scheme">The name of the authentication scheme.</param>
124+
/// <param name="principal">The user.</param>
125+
/// <param name="properties">The <see cref="AuthenticationProperties"/> properties.</param>
126+
/// <returns>The task.</returns>
127+
public static Task SignInAsync(this HttpContext context, string scheme, ClaimsPrincipal principal, AuthenticationProperties properties) =>
128+
context.RequestServices.GetRequiredService<IAuthenticationService>().SignInAsync(context, scheme, principal, properties);
129+
130+
/// <summary>
131+
/// Extension method for SignOut.
132+
/// </summary>
133+
/// <param name="context">The <see cref="HttpContext"/> context.</param>
134+
/// <param name="scheme">The name of the authentication scheme.</param>
135+
/// <returns>The task.</returns>
136+
public static Task SignOutAsync(this HttpContext context, string scheme) => context.SignOutAsync(scheme, properties: null);
137+
138+
/// <summary>
139+
/// Extension method for SignOut.
140+
/// </summary>
141+
/// <param name="context">The <see cref="HttpContext"/> context.</param>
142+
/// <param name="scheme">The name of the authentication scheme.</param>
143+
/// <param name="properties">The <see cref="AuthenticationProperties"/> properties.</param>
144+
/// <returns></returns>
145+
public static Task SignOutAsync(this HttpContext context, string scheme, AuthenticationProperties properties) =>
146+
context.RequestServices.GetRequiredService<IAuthenticationService>().SignOutAsync(context, scheme, properties);
147+
148+
/// <summary>
149+
/// Extension method for getting the value of an authentication token.
150+
/// </summary>
151+
/// <param name="context">The <see cref="HttpContext"/> context.</param>
152+
/// <param name="scheme">The name of the authentication scheme.</param>
153+
/// <param name="tokenName">The name of the token.</param>
154+
/// <returns>The value of the token.</returns>
155+
public static Task<string> GetTokenAsync(this HttpContext context, string scheme, string tokenName) =>
156+
context.RequestServices.GetRequiredService<IAuthenticationService>().GetTokenAsync(context, scheme, tokenName);
157+
}
158+
}

0 commit comments

Comments
 (0)