zhaolei
2020-11-20 4a2e5b9a21940f11757be37d99f0944e240e908b
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
using Prow.WeChatAuth;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.Configuration;
using System;
 
namespace Microsoft.Extensions.DependencyInjection
{
    /// <summary>
    /// Gitee 授权认证服务扩展类
    /// </summary>
    public static class WeChatExtensions
    {
        /// <summary>
        /// 添加 Gitee 认证授权服务
        /// </summary>
        /// <param name="builder">认证实例</param>
        /// <param name="configureOptions">GiteeAuthenticationOptions 权限配置项回调方法</param>
        /// <returns>AuthenticationBuilder 实例</returns>
        public static AuthenticationBuilder AddWeChat(this AuthenticationBuilder builder, Action<WeChatOptions>? configureOptions = null) => builder.AddWeChat(WeChatDefaults.AuthenticationScheme, WeChatDefaults.DisplayName, configureOptions);
 
        /// <summary>
        /// 添加 Gitee 认证授权服务
        /// </summary>
        /// <param name="builder">认证实例</param>
        /// <param name="authenticationScheme">Gitee 认证架构名称 默认值为 Gitee</param>
        /// <param name="configureOptions">GiteeAuthenticationOptions 权限配置项回调方法</param>
        /// <returns>AuthenticationBuilder 实例</returns>
        public static AuthenticationBuilder AddWeChat(this AuthenticationBuilder builder, string authenticationScheme, Action<WeChatOptions>? configureOptions = null) => builder.AddWeChat(authenticationScheme, WeChatDefaults.DisplayName, configureOptions);
 
        /// <summary>
        /// 添加 Gitee 认证授权服务
        /// </summary>
        /// <param name="builder">认证实例</param>
        /// <param name="authenticationScheme">Gitee 认证架构名称 默认值为 Gitee</param>
        /// <param name="displayName">Gitee 显示名称 默认值为 Gitee</param>
        /// <param name="configureOptions">GiteeAuthenticationOptions 权限配置项回调方法</param>
        /// <returns>AuthenticationBuilder 实例</returns>
        public static AuthenticationBuilder AddWeChat(this AuthenticationBuilder builder, string authenticationScheme, string displayName, Action<WeChatOptions>? configureOptions)
        {
            var config = builder.Services.BuildServiceProvider().GetRequiredService<IConfiguration>();
            builder.Services.Configure<WeChatOptions>(authenticationScheme, config.GetSection(nameof(WeChatOptions)));
            return OAuthExtensions.AddOAuth<WeChatOptions, WeChatHandler>(builder, authenticationScheme, displayName, configureOptions);
        }
    }
}