zhaolei
7 days ago 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
59
60
61
using Bootstrap.Security.Mvc;
using Microsoft.AspNetCore.WebUtilities;
using System;
using System.Collections.Generic;
 
namespace Microsoft.AspNetCore.Authentication.Cookies
{
    /// <summary>
    /// CookieAuthenticationOptions 扩展类
    /// </summary>
    public static class CookieAuthenticationOptionsExtensions
    {
        /// <summary>
        /// 重写OnRedirectToLogin/OnRedirectToAccessDenied方法
        /// </summary>
        /// <param name="options">CookieAuthenticationOptions 实例</param>
        /// <param name="authHost">授权网站主机头</param>
        /// <param name="loginPath">登录地址,默认为 CookieAuthenticationDefaults.LoginPath</param>
        /// <param name="accessDeniedPath">拒绝访问地址,默认为 CookieAuthenticationDefaults.AccessDeniedPath</param>
        public static void RebuildRedirectUri(this CookieAuthenticationOptions options, string authHost, string? loginPath = null, string? accessDeniedPath = null)
        {
            var originRedirectToLogin = options.Events.OnRedirectToLogin;
            options.Events.OnRedirectToLogin = context =>
            {
                return originRedirectToLogin(context.RebuildRedirectUri(authHost, loginPath ?? CookieAuthenticationDefaults.LoginPath.Value));
            };
 
            var originRedirectToAccessDenied = options.Events.OnRedirectToAccessDenied;
            options.Events.OnRedirectToAccessDenied = context =>
            {
                return originRedirectToAccessDenied(context.RebuildRedirectUri(authHost, accessDeniedPath ?? CookieAuthenticationDefaults.AccessDeniedPath));
            };
        }
 
        private static RedirectContext<CookieAuthenticationOptions> RebuildRedirectUri(this RedirectContext<CookieAuthenticationOptions> context, string authHost, string url)
        {
            if (context.RedirectUri.StartsWith(authHost)) return context;
 
            var uriBuilder = new UriBuilder(authHost);
            uriBuilder.Path = uriBuilder.Path.TrimEnd('/') + url;
            context.RedirectUri = RebuildUriQuery(context, uriBuilder);
            return context;
        }
 
        private static string RebuildUriQuery(RedirectContext<CookieAuthenticationOptions> context, UriBuilder uriBuilder)
        {
            // 获得原始认证地址 http://localhost:49185/Account/Login?ReturnUrl=%2FHome%2FAbout%3FTest1%3D1%26Test2%3D2
            var originUri = new Uri(context.RedirectUri);
            var queryStrings = QueryHelpers.ParseQuery(originUri.Query);
 
            // 新返回地址 http://localhost:49185/Home/About?Test1=1&Test2=2
            var returnUrl = originUri.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped) + queryStrings[context.Options.ReturnUrlParameter];
 
            // 拼装新的认证地址 http://localhost:50852/Account/Login?ReturnUrl=http%3A%2F%2Flocalhost%3A49185%2FHome%2FAbout%3FTest1%3D1%26Test2%3D2&AppId=2
            var querys = new Dictionary<string, string>();
            querys.Add(context.Options.ReturnUrlParameter, returnUrl);
            querys.Add("AppId", BootstrapAppContext.AppId);
            return QueryHelpers.AddQueryString(uriBuilder.ToString(), querys);
        }
    }
}