From 56666e349c4655e43923ab7ed2f5808e827d6acd Mon Sep 17 00:00:00 2001 From: Hao Kung Date: Tue, 20 Mar 2018 09:53:34 -0700 Subject: [PATCH] Add AddDefaultIdentity --- samples/IdentitySample.DefaultUI/Startup.cs | 13 +--- .../IdentityServiceCollectionUIExtensions.cs | 65 +++++++++++++++++++ .../Identity.DefaultUI.WebSite/Startup.cs | 13 +--- 3 files changed, 69 insertions(+), 22 deletions(-) create mode 100644 src/UI/IdentityServiceCollectionUIExtensions.cs diff --git a/samples/IdentitySample.DefaultUI/Startup.cs b/samples/IdentitySample.DefaultUI/Startup.cs index cfa5dfca..76962038 100644 --- a/samples/IdentitySample.DefaultUI/Startup.cs +++ b/samples/IdentitySample.DefaultUI/Startup.cs @@ -34,18 +34,9 @@ namespace IdentitySample.DefaultUI services.AddMvc(); - services.AddIdentityCore(o => o.Stores.MaxLengthForKeys = 128) + services.AddDefaultIdentity() .AddRoles() - .AddEntityFrameworkStores() - .AddDefaultUI() - .AddDefaultTokenProviders(); - - services.AddAuthentication(o => - { - o.DefaultScheme = IdentityConstants.ApplicationScheme; - o.DefaultSignInScheme = IdentityConstants.ExternalScheme; - }) - .AddIdentityCookies(o => { }); + .AddEntityFrameworkStores(); } diff --git a/src/UI/IdentityServiceCollectionUIExtensions.cs b/src/UI/IdentityServiceCollectionUIExtensions.cs new file mode 100644 index 00000000..65eecc8e --- /dev/null +++ b/src/UI/IdentityServiceCollectionUIExtensions.cs @@ -0,0 +1,65 @@ +// 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.IO; +using System.Linq; +using System.Reflection; +using Microsoft.AspNetCore.Identity.UI; +using Microsoft.AspNetCore.Identity.UI.Services; +using Microsoft.AspNetCore.Mvc.ApplicationParts; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.DependencyInjection.Extensions; + +namespace Microsoft.AspNetCore.Identity +{ + /// + /// Default UI extensions to . + /// + public static class IdentityServiceCollectionUIExtensions + { + /// + /// Adds a set of common identity services to the application, including a default UI, token providers, + /// and configures authentication to use identity cookies. + /// + /// + /// In order to use the default UI, the application must be using , + /// and contain a _LoginPartial partial view that + /// can be found by the application. + /// + /// The . + /// The . + public static IdentityBuilder AddDefaultIdentity(this IServiceCollection services) where TUser : class + => services.AddDefaultIdentity(_ => { }); + + /// + /// Adds a set of common identity services to the application, including a default UI, token providers, + /// and configures authentication to use identity cookies. + /// + /// + /// In order to use the default UI, the application must be using , + /// and contain a _LoginPartial partial view that + /// can be found by the application. + /// + /// The . + /// Configures the . + /// The . + public static IdentityBuilder AddDefaultIdentity(this IServiceCollection services, Action configureOptions) where TUser : class + { + services.AddAuthentication(o => + { + o.DefaultScheme = IdentityConstants.ApplicationScheme; + o.DefaultSignInScheme = IdentityConstants.ExternalScheme; + }) + .AddIdentityCookies(o => { }); + + return services.AddIdentityCore(o => + { + o.Stores.MaxLengthForKeys = 128; + configureOptions?.Invoke(o); + }) + .AddDefaultUI() + .AddDefaultTokenProviders(); + } + } +} \ No newline at end of file diff --git a/test/WebSites/Identity.DefaultUI.WebSite/Startup.cs b/test/WebSites/Identity.DefaultUI.WebSite/Startup.cs index 95231fea..7e9ff05c 100644 --- a/test/WebSites/Identity.DefaultUI.WebSite/Startup.cs +++ b/test/WebSites/Identity.DefaultUI.WebSite/Startup.cs @@ -35,18 +35,9 @@ namespace Identity.DefaultUI.WebSite sqlOptions => sqlOptions.MigrationsAssembly("Identity.DefaultUI.WebSite") )); - services.AddIdentityCore() + services.AddDefaultIdentity() .AddRoles() - .AddEntityFrameworkStores() - .AddDefaultUI() - .AddDefaultTokenProviders(); - - services.AddAuthentication(o => - { - o.DefaultScheme = IdentityConstants.ApplicationScheme; - o.DefaultSignInScheme = IdentityConstants.ExternalScheme; - }) - .AddIdentityCookies(o => {}); + .AddEntityFrameworkStores(); services.AddMvc() .AddRazorPagesOptions(options =>