Skip to content

Commit e755f60

Browse files
pranavkmHaoKTratcher
authored
Add docs for Auth, Auth.Cookies, Auth.Certificate (#26503)
* Add docs for Auth, Auth.Cookies, Auth.Certificate Contributes to #26397 * Also add JWT * Apply suggestions from code review Co-authored-by: Hao Kung <[email protected]> Co-authored-by: Chris Ross <[email protected]> * Update src/Security/Authentication/Core/src/TicketSerializer.cs * Update src/Security/Authentication/Core/src/TicketSerializer.cs * Update src/Security/Authentication/Core/src/TicketSerializer.cs * Apply suggestions from code review Co-authored-by: Hao Kung <[email protected]> Co-authored-by: Chris Ross <[email protected]>
1 parent acf1dc4 commit e755f60

File tree

50 files changed

+650
-57
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+650
-57
lines changed

src/Security/Authentication/Certificate/src/CertificateAuthenticationExtensions.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Security.Claims;
56
using Microsoft.AspNetCore.Authentication;
67

78
using Microsoft.AspNetCore.Authentication.Certificate;
@@ -15,6 +16,11 @@ public static class CertificateAuthenticationAppBuilderExtensions
1516
{
1617
/// <summary>
1718
/// Adds certificate authentication.
19+
/// <para>
20+
/// Certificate authentication uses a authentication handler that validates client certificate and
21+
/// raises an event where the certificate is resolved to a <see cref="ClaimsPrincipal"/>.
22+
/// See https://tools.ietf.org/html/rfc5246#section-7.4.4 to read more about certicate authentication.
23+
/// </para>
1824
/// </summary>
1925
/// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param>
2026
/// <returns>The <see cref="AuthenticationBuilder"/>.</returns>
@@ -23,6 +29,11 @@ public static AuthenticationBuilder AddCertificate(this AuthenticationBuilder bu
2329

2430
/// <summary>
2531
/// Adds certificate authentication.
32+
/// <para>
33+
/// Certificate authentication uses a authentication handler that validates client certificate and
34+
/// raises an event where the certificate is resolved to a <see cref="ClaimsPrincipal"/>.
35+
/// See https://tools.ietf.org/html/rfc5246#section-7.4.4 to read more about certicate authentication.
36+
/// </para>
2637
/// </summary>
2738
/// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param>
2839
/// <param name="authenticationScheme"></param>
@@ -32,6 +43,11 @@ public static AuthenticationBuilder AddCertificate(this AuthenticationBuilder bu
3243

3344
/// <summary>
3445
/// Adds certificate authentication.
46+
/// <para>
47+
/// Certificate authentication uses a authentication handler that validates client certificate and
48+
/// raises an event where the certificate is resolved to a <see cref="ClaimsPrincipal"/>.
49+
/// See https://tools.ietf.org/html/rfc5246#section-7.4.4 to read more about certicate authentication.
50+
/// </para>
3551
/// </summary>
3652
/// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param>
3753
/// <param name="configureOptions"></param>
@@ -41,6 +57,11 @@ public static AuthenticationBuilder AddCertificate(this AuthenticationBuilder bu
4157

4258
/// <summary>
4359
/// Adds certificate authentication.
60+
/// <para>
61+
/// Certificate authentication uses a authentication handler that validates client certificate and
62+
/// raises an event where the certificate is resolved to a <see cref="ClaimsPrincipal"/>.
63+
/// See https://tools.ietf.org/html/rfc5246#section-7.4.4 to read more about certicate authentication.
64+
/// </para>
4465
/// </summary>
4566
/// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param>
4667
/// <param name="authenticationScheme"></param>
@@ -54,6 +75,11 @@ public static AuthenticationBuilder AddCertificate(
5475

5576
/// <summary>
5677
/// Adds certificate authentication.
78+
/// <para>
79+
/// Certificate authentication uses a authentication handler that validates client certificate and
80+
/// raises an event where the certificate is resolved to a <see cref="ClaimsPrincipal"/>.
81+
/// See https://tools.ietf.org/html/rfc5246#section-7.4.4 to read more about certicate authentication.
82+
/// </para>
5783
/// </summary>
5884
/// <param name="builder">The <see cref="AuthenticationBuilder"/>.</param>
5985
/// <param name="configureOptions"></param>

src/Security/Authentication/Certificate/src/CertificateAuthenticationOptions.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public class CertificateAuthenticationOptions : AuthenticationSchemeOptions
1313
/// <summary>
1414
/// Value indicating the types of certificates accepted by the authentication middleware.
1515
/// </summary>
16+
/// <value>
17+
/// Defaults to <see cref="CertificateTypes.Chained"/>.
18+
/// </value>
1619
public CertificateTypes AllowedCertificateTypes { get; set; } = CertificateTypes.Chained;
1720

1821
/// <summary>
@@ -23,6 +26,9 @@ public class CertificateAuthenticationOptions : AuthenticationSchemeOptions
2326
/// <summary>
2427
/// Method used to validate certificate chains against <see cref="CustomTrustStore"/>.
2528
/// </summary>
29+
/// <value>
30+
/// Defaults to <see cref="X509ChainTrustMode.System"/>.
31+
/// </value>
2632
/// <remarks>This property must be set to <see cref="X509ChainTrustMode.CustomRootTrust"/> to enable <see cref="CustomTrustStore"/> to be used in certificate chain validation.</remarks>
2733
public X509ChainTrustMode ChainTrustValidationMode { get; set; } = X509ChainTrustMode.System;
2834

@@ -32,21 +38,33 @@ public class CertificateAuthenticationOptions : AuthenticationSchemeOptions
3238
/// at all. If the certificate chains to a root CA all certificates in the chain must be validated
3339
/// for the client authentication EKU.
3440
/// </summary>
41+
/// <value>
42+
/// Defaults to <see langword="true" />.
43+
/// </value>
3544
public bool ValidateCertificateUse { get; set; } = true;
3645

3746
/// <summary>
3847
/// Flag indicating whether the client certificate validity period should be checked.
3948
/// </summary>
49+
/// <value>
50+
/// Defaults to <see langword="true" />.
51+
/// </value>
4052
public bool ValidateValidityPeriod { get; set; } = true;
4153

4254
/// <summary>
4355
/// Specifies which X509 certificates in the chain should be checked for revocation.
4456
/// </summary>
57+
/// <value>
58+
/// Defaults to <see cref="X509RevocationFlag.ExcludeRoot" />.
59+
/// </value>
4560
public X509RevocationFlag RevocationFlag { get; set; } = X509RevocationFlag.ExcludeRoot;
4661

4762
/// <summary>
4863
/// Specifies conditions under which verification of certificates in the X509 chain should be conducted.
4964
/// </summary>
65+
/// <value>
66+
/// Defaults to <see cref="X509RevocationMode.Online" />.
67+
/// </value>
5068
public X509RevocationMode RevocationMode { get; set; } = X509RevocationMode.Online;
5169

5270
/// <summary>

src/Security/Authentication/Certificate/src/CertificateValidationCache.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ namespace Microsoft.AspNetCore.Authentication.Certificate
1414
/// </summary>
1515
public class CertificateValidationCache : ICertificateValidationCache
1616
{
17-
private MemoryCache _cache;
18-
private CertificateValidationCacheOptions _options;
17+
private readonly MemoryCache _cache;
18+
private readonly CertificateValidationCacheOptions _options;
1919

20+
/// <summary>
21+
/// Initializes a new instance of <see cref="CertificateValidationCache"/>.
22+
/// </summary>
23+
/// <param name="options">An accessor to <see cref="CertificateValidationCacheOptions"/></param>
2024
public CertificateValidationCache(IOptions<CertificateValidationCacheOptions> options)
2125
{
2226
_options = options.Value;
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4-
// Copyright (c) .NET Foundation. All rights reserved.
5-
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
6-
74
using System;
85

96
namespace Microsoft.AspNetCore.Authentication.Certificate
@@ -14,14 +11,18 @@ namespace Microsoft.AspNetCore.Authentication.Certificate
1411
public class CertificateValidationCacheOptions
1512
{
1613
/// <summary>
17-
/// The expiration that should be used for entries in the MemoryCache, defaults to 2 minutes.
14+
/// Gets or sets the expiration that should be used for entries in the MemoryCache.
1815
/// This is a sliding expiration that will extend each time the certificate is used, so long as the certificate is valid (see X509Certificate2.NotAfter).
1916
/// </summary>
17+
/// <value>Defaults to 2 minutes.</value>
2018
public TimeSpan CacheEntryExpiration { get; set; } = TimeSpan.FromMinutes(2);
2119

2220
/// <summary>
23-
/// How many validated certificate results to store in the cache, defaults to 1024.
21+
/// Gets or sets the maximum number of validated certificate results that are allowed to cached.
2422
/// </summary>
23+
/// <value>
24+
/// Defaults to 1024.
25+
/// </value>
2526
public int CacheSize { get; set; } = 1024;
2627
}
2728
}

src/Security/Authentication/Certificate/src/Microsoft.AspNetCore.Authentication.Certificate.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<Description>ASP.NET Core middleware that enables an application to support certificate authentication.</Description>
55
<TargetFramework>$(DefaultNetCoreTargetFramework)</TargetFramework>
66
<DefineConstants>$(DefineConstants);SECURITY</DefineConstants>
77
<GenerateDocumentationFile>true</GenerateDocumentationFile>
88
<PackageTags>aspnetcore;authentication;security;x509;certificate</PackageTags>
9+
<NoWarn>$(NoWarn.Replace('1591', ''))</NoWarn>
910
</PropertyGroup>
1011

1112
<ItemGroup>

src/Security/Authentication/Cookies/src/CookieAuthenticationEvents.cs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,32 @@
99
namespace Microsoft.AspNetCore.Authentication.Cookies
1010
{
1111
/// <summary>
12-
/// This default implementation of the ICookieAuthenticationEvents may be used if the
13-
/// application only needs to override a few of the interface methods. This may be used as a base class
14-
/// or may be instantiated directly.
12+
/// Allows subscribing to events raised during cookie authentication.
1513
/// </summary>
1614
public class CookieAuthenticationEvents
1715
{
1816
/// <summary>
19-
/// A delegate assigned to this property will be invoked when the related method is called.
17+
/// Invoked to validate the principal.
2018
/// </summary>
2119
public Func<CookieValidatePrincipalContext, Task> OnValidatePrincipal { get; set; } = context => Task.CompletedTask;
2220

2321
/// <summary>
24-
/// A delegate assigned to this property will be invoked when the related method is called.
22+
/// Invoked on signing in.
2523
/// </summary>
2624
public Func<CookieSigningInContext, Task> OnSigningIn { get; set; } = context => Task.CompletedTask;
2725

2826
/// <summary>
29-
/// A delegate assigned to this property will be invoked when the related method is called.
27+
/// Invoked after sign in has completed.
3028
/// </summary>
3129
public Func<CookieSignedInContext, Task> OnSignedIn { get; set; } = context => Task.CompletedTask;
3230

3331
/// <summary>
34-
/// A delegate assigned to this property will be invoked when the related method is called.
32+
/// Invoked on signing out.
3533
/// </summary>
3634
public Func<CookieSigningOutContext, Task> OnSigningOut { get; set; } = context => Task.CompletedTask;
3735

3836
/// <summary>
39-
/// A delegate assigned to this property will be invoked when the related method is called.
37+
/// Invoked when the client needs to be redirected to the sign in url.
4038
/// </summary>
4139
public Func<RedirectContext<CookieAuthenticationOptions>, Task> OnRedirectToLogin { get; set; } = context =>
4240
{
@@ -53,7 +51,7 @@ public class CookieAuthenticationEvents
5351
};
5452

5553
/// <summary>
56-
/// A delegate assigned to this property will be invoked when the related method is called.
54+
/// Invoked when the client needs to be redirected to the access denied url.
5755
/// </summary>
5856
public Func<RedirectContext<CookieAuthenticationOptions>, Task> OnRedirectToAccessDenied { get; set; } = context =>
5957
{
@@ -70,7 +68,7 @@ public class CookieAuthenticationEvents
7068
};
7169

7270
/// <summary>
73-
/// A delegate assigned to this property will be invoked when the related method is called.
71+
/// Invoked when the client is to be redirected to logout.
7472
/// </summary>
7573
public Func<RedirectContext<CookieAuthenticationOptions>, Task> OnRedirectToLogout { get; set; } = context =>
7674
{
@@ -86,7 +84,7 @@ public class CookieAuthenticationEvents
8684
};
8785

8886
/// <summary>
89-
/// A delegate assigned to this property will be invoked when the related method is called.
87+
/// Invoked when the client is to be redirected after logout.
9088
/// </summary>
9189
public Func<RedirectContext<CookieAuthenticationOptions>, Task> OnRedirectToReturnUrl { get; set; } = context =>
9290
{
@@ -108,52 +106,51 @@ private static bool IsAjaxRequest(HttpRequest request)
108106
}
109107

110108
/// <summary>
111-
/// Implements the interface method by invoking the related delegate method.
109+
/// Invoked to validate the prinicipal.
112110
/// </summary>
113-
/// <param name="context"></param>
114-
/// <returns></returns>
111+
/// <param name="context">The <see cref="CookieValidatePrincipalContext"/>.</param>
115112
public virtual Task ValidatePrincipal(CookieValidatePrincipalContext context) => OnValidatePrincipal(context);
116113

117114
/// <summary>
118-
/// Implements the interface method by invoking the related delegate method.
115+
/// Invoked during sign in.
119116
/// </summary>
120-
/// <param name="context"></param>
117+
/// <param name="context">The <see cref="CookieSigningInContext"/>.</param>
121118
public virtual Task SigningIn(CookieSigningInContext context) => OnSigningIn(context);
122119

123120
/// <summary>
124-
/// Implements the interface method by invoking the related delegate method.
121+
/// Invoked after sign in has completed.
125122
/// </summary>
126-
/// <param name="context"></param>
123+
/// <param name="context">The <see cref="CookieSignedInContext"/>.</param>
127124
public virtual Task SignedIn(CookieSignedInContext context) => OnSignedIn(context);
128125

129126
/// <summary>
130-
/// Implements the interface method by invoking the related delegate method.
127+
/// Invoked on sign out.
131128
/// </summary>
132-
/// <param name="context"></param>
129+
/// <param name="context">The <see cref="CookieSigningOutContext"/>.</param>
133130
public virtual Task SigningOut(CookieSigningOutContext context) => OnSigningOut(context);
134131

135132
/// <summary>
136-
/// Implements the interface method by invoking the related delegate method.
133+
/// Invoked when the client is being redirected to the log out url.
137134
/// </summary>
138-
/// <param name="context">Contains information about the event</param>
135+
/// <param name="context">The <see cref="RedirectContext{TOptions}"/>.</param>
139136
public virtual Task RedirectToLogout(RedirectContext<CookieAuthenticationOptions> context) => OnRedirectToLogout(context);
140137

141138
/// <summary>
142-
/// Implements the interface method by invoking the related delegate method.
139+
/// Invoked when the client is being redirected to the log in url.
143140
/// </summary>
144-
/// <param name="context">Contains information about the event</param>
141+
/// <param name="context">The <see cref="RedirectContext{TOptions}"/>.</param>
145142
public virtual Task RedirectToLogin(RedirectContext<CookieAuthenticationOptions> context) => OnRedirectToLogin(context);
146143

147144
/// <summary>
148-
/// Implements the interface method by invoking the related delegate method.
145+
/// Invoked when the client is being redirected after log out.
149146
/// </summary>
150-
/// <param name="context">Contains information about the event</param>
147+
/// <param name="context">The <see cref="RedirectContext{TOptions}"/>.</param>
151148
public virtual Task RedirectToReturnUrl(RedirectContext<CookieAuthenticationOptions> context) => OnRedirectToReturnUrl(context);
152149

153150
/// <summary>
154-
/// Implements the interface method by invoking the related delegate method.
151+
/// Invoked when the client is being redirected to the access denied url.
155152
/// </summary>
156-
/// <param name="context">Contains information about the event</param>
153+
/// <param name="context">The <see cref="RedirectContext{TOptions}"/>.</param>
157154
public virtual Task RedirectToAccessDenied(RedirectContext<CookieAuthenticationOptions> context) => OnRedirectToAccessDenied(context);
158155
}
159156
}

src/Security/Authentication/Cookies/src/CookieAuthenticationHandler.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
namespace Microsoft.AspNetCore.Authentication.Cookies
1717
{
18+
/// <summary>
19+
/// Implementation for the cookie-based authentication handler.
20+
/// </summary>
1821
public class CookieAuthenticationHandler : SignInAuthenticationHandler<CookieAuthenticationOptions>
1922
{
2023
private const string HeaderValueNoCache = "no-cache";
@@ -32,6 +35,13 @@ public class CookieAuthenticationHandler : SignInAuthenticationHandler<CookieAut
3235
private Task<AuthenticateResult>? _readCookieTask;
3336
private AuthenticationTicket? _refreshTicket;
3437

38+
/// <summary>
39+
/// Initializes a new instance of <see cref="CookieAuthenticationHandler"/>.
40+
/// </summary>
41+
/// <param name="options">Accessor to <see cref="CookieAuthenticationOptions"/>.</param>
42+
/// <param name="logger">The <see cref="ILoggerFactory"/>.</param>
43+
/// <param name="encoder">The <see cref="UrlEncoder"/>.</param>
44+
/// <param name="clock">The <see cref="ISystemClock"/>.</param>
3545
public CookieAuthenticationHandler(IOptionsMonitor<CookieAuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock)
3646
: base(options, logger, encoder, clock)
3747
{ }
@@ -46,6 +56,7 @@ public CookieAuthenticationHandler(IOptionsMonitor<CookieAuthenticationOptions>
4656
set { base.Events = value; }
4757
}
4858

59+
/// <inheritdoc />
4960
protected override Task InitializeHandlerAsync()
5061
{
5162
// Cookies needs to finish the response
@@ -169,6 +180,7 @@ private async Task<AuthenticateResult> ReadCookieTicket()
169180
return AuthenticateResult.Success(ticket);
170181
}
171182

183+
/// <inheritdoc />
172184
protected override async Task<AuthenticateResult> HandleAuthenticateAsync()
173185
{
174186
var result = await EnsureCookieTicket();
@@ -203,6 +215,7 @@ private CookieOptions BuildCookieOptions()
203215
return cookieOptions;
204216
}
205217

218+
/// <inheritdoc />
206219
protected virtual async Task FinishResponseAsync()
207220
{
208221
// Only renew if requested, and neither sign in or sign out was called
@@ -254,6 +267,7 @@ protected virtual async Task FinishResponseAsync()
254267
}
255268
}
256269

270+
/// <inheritdoc />
257271
protected async override Task HandleSignInAsync(ClaimsPrincipal user, AuthenticationProperties? properties)
258272
{
259273
if (user == null)
@@ -346,6 +360,7 @@ protected async override Task HandleSignInAsync(ClaimsPrincipal user, Authentica
346360
Logger.AuthenticationSchemeSignedIn(Scheme.Name);
347361
}
348362

363+
/// <inheritdoc />
349364
protected async override Task HandleSignOutAsync(AuthenticationProperties? properties)
350365
{
351366
properties = properties ?? new AuthenticationProperties();
@@ -426,6 +441,7 @@ private static bool IsHostRelative(string path)
426441
return path[0] == '/' && path[1] != '/' && path[1] != '\\';
427442
}
428443

444+
/// <inheritdoc />
429445
protected override async Task HandleForbiddenAsync(AuthenticationProperties properties)
430446
{
431447
var returnUrl = properties.RedirectUri;
@@ -438,6 +454,7 @@ protected override async Task HandleForbiddenAsync(AuthenticationProperties prop
438454
await Events.RedirectToAccessDenied(redirectContext);
439455
}
440456

457+
/// <inheritdoc />
441458
protected override async Task HandleChallengeAsync(AuthenticationProperties properties)
442459
{
443460
var redirectUri = properties.RedirectUri;

0 commit comments

Comments
 (0)