zhaolei
2020-11-20 921de2254ff5712a44ed8575ee8efe34252f6603
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
57
58
using Bootstrap.Security.Mvc;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.IO;
 
namespace Microsoft.AspNetCore.Builder
{
    /// <summary>
    /// BootstrapAdminAuthorization 认证服务扩展类
    /// </summary>
    public static class AuthenticationServiceCollectionExtensions
    {
        /// <summary>
        /// 添加 BootstrapAdmin 认证授权服务,内部调用 AddAuthentication
        /// </summary>
        /// <param name="services">服务容器集合</param>
        /// <param name="configuration">IConfiguration 实例</param>
        /// <param name="configureOptions">BootstrapAdminAuthenticationOptions 配置回调方法</param>
        /// <param name="configureCookies">CookieAuthenticationOptions 配置回调方法</param>
        /// <returns>IServiceCollection 服务容器集合</returns>
        public static AuthenticationBuilder AddBootstrapAdminAuthentication(this IServiceCollection services, IConfiguration configuration, Action<BootstrapAdminAuthenticationOptions>? configureOptions = null, Action<CookieAuthenticationOptions>? configureCookies = null)
        {
            BootstrapAppContext.SetConfiguration(configuration);
 
            // 获取当前应用 AppId
            if (string.IsNullOrEmpty(BootstrapAppContext.AppId)) throw new InvalidOperationException("Please set AppId value in config file.");
 
            var options = configuration.GetOption(() => new BootstrapAdminAuthenticationOptions());
            options.KeyPath = options.KeyPath.GetOSPlatformPath();
 
            // config BootstrapAdminOptions
            configureOptions?.Invoke(options);
            return AddAuthentication(services, options, configureCookies);
        }
 
        private static AuthenticationBuilder AddAuthentication(IServiceCollection services, BootstrapAdminAuthenticationOptions options, Action<CookieAuthenticationOptions>? configure = null)
        {
            // set sso
            var dataProtectionBuilder = services.AddDataProtection(op => op.ApplicationDiscriminator = options.ApplicationDiscriminator)
                .SetApplicationName(options.ApplicationAppName)
                .PersistKeysToFileSystem(new DirectoryInfo(options.KeyPath));
            if (!string.IsNullOrEmpty(options.AuthHost)) dataProtectionBuilder.DisableAutomaticKeyGeneration();
 
            // Authentication && Cookie
            return services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie(op =>
            {
                if (!string.IsNullOrEmpty(options.CookiePath)) op.Cookie.Path = options.CookiePath;
                if (!string.IsNullOrEmpty(options.CookieDomain)) op.Cookie.Domain = options.CookieDomain;
                if (!string.IsNullOrEmpty(options.AuthHost)) op.RebuildRedirectUri(options.AuthHost);
                configure?.Invoke(op);
            });
        }
    }
}