-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
IdentityServiceCollectionUIExtensions.cs
56 lines (51 loc) · 2.51 KB
/
IdentityServiceCollectionUIExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.AspNetCore.Identity;
namespace Microsoft.Extensions.DependencyInjection;
/// <summary>
/// Default UI extensions to <see cref="IServiceCollection"/>.
/// </summary>
public static class IdentityServiceCollectionUIExtensions
{
/// <summary>
/// Adds a set of common identity services to the application, including a default UI, token providers,
/// and configures authentication to use identity cookies.
/// </summary>
/// <remarks>
/// In order to use the default UI, the application must be using <see cref="Microsoft.AspNetCore.Mvc"/>,
/// <see cref="Microsoft.AspNetCore.StaticFiles"/> and contain a <c>_LoginPartial</c> partial view that
/// can be found by the application.
/// </remarks>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <returns>The <see cref="IdentityBuilder"/>.</returns>
public static IdentityBuilder AddDefaultIdentity<TUser>(this IServiceCollection services) where TUser : class
=> services.AddDefaultIdentity<TUser>(_ => { });
/// <summary>
/// Adds a set of common identity services to the application, including a default UI, token providers,
/// and configures authentication to use identity cookies.
/// </summary>
/// <remarks>
/// In order to use the default UI, the application must be using <see cref="Microsoft.AspNetCore.Mvc"/>,
/// <see cref="Microsoft.AspNetCore.StaticFiles"/> and contain a <c>_LoginPartial</c> partial view that
/// can be found by the application.
/// </remarks>
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
/// <param name="configureOptions">Configures the <see cref="IdentityOptions"/>.</param>
/// <returns>The <see cref="IdentityBuilder"/>.</returns>
public static IdentityBuilder AddDefaultIdentity<TUser>(this IServiceCollection services, Action<IdentityOptions> configureOptions) where TUser : class
{
services.AddAuthentication(o =>
{
o.DefaultScheme = IdentityConstants.ApplicationScheme;
o.DefaultSignInScheme = IdentityConstants.ExternalScheme;
})
.AddIdentityCookies(o => { });
return services.AddIdentityCore<TUser>(o =>
{
o.Stores.MaxLengthForKeys = 128;
configureOptions?.Invoke(o);
})
.AddDefaultUI()
.AddDefaultTokenProviders();
}
}