bklLiudl
2024-05-25 484e5129e4c9a671c5660a556a24bd306f1fdd9b
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Wms_09.Tools;
 
namespace Wms_09.Filter
{
    /// <summary>
    /// MyAuthHandler
    /// </summary>
    public class MyAuthHandler : IAuthenticationHandler
    {
        private ITokenHelper tokenHelper;
        public MyAuthHandler(ITokenHelper _tokenHelper) //通过依赖注入得到数据访问层实例
        {
            tokenHelper = _tokenHelper;
        }
        /// <summary>
        /// 
        /// </summary>
        public const string SchemeName = "MyAuth";
 
        AuthenticationScheme _scheme;
        HttpContext _context;
 
        /// <summary>
        /// 初始化认证
        /// </summary>
        public Task InitializeAsync(AuthenticationScheme scheme, HttpContext context)
        {
            _scheme = scheme;
            _context = context;
            return Task.CompletedTask;
        }
 
        /// <summary>
        /// 认证处理
        /// </summary>
        public Task<AuthenticateResult> AuthenticateAsync()
        {
            var req = _context.Request.Headers;
            var isLogin = req["token"].FirstOrDefault();
            if (isLogin == null)
            {
                return Task.FromResult(AuthenticateResult.Fail("未登陆"));
            }
 
            string userId = "";
            TokenType tokenType = tokenHelper.ValiTokenState(isLogin, a => a["iss"] == "WYY" && a["aud"] == "EveryTestOne", action => { userId = action["loginID"]; });
            if (tokenType == TokenType.Fail)
            {
                return Task.FromResult(AuthenticateResult.Fail("验证失败"));
            }
            if (tokenType == TokenType.Expired)
            {
                return Task.FromResult(AuthenticateResult.Fail("token值过期"));
            }
 
            //_context.ActionArguments.Add("userId", Convert.ToInt32(userId));
            
 
            var ticket = GetAuthTicket(userId, userId);
            var data = AuthenticateResult.Success(ticket);
            return Task.FromResult(data);
 
        }
 
        AuthenticationTicket GetAuthTicket(string name, string role)
        {
            var claimsIdentity = new ClaimsIdentity(new Claim[]
            {
                new Claim(ClaimTypes.Name, name),
                new Claim(ClaimTypes.Role, role),
            }, "My_Auth");
 
            var principal = new ClaimsPrincipal(claimsIdentity);
            return new AuthenticationTicket(principal, _scheme.Name);
        }
 
        /// <summary>
        /// 权限不足时的处理
        /// </summary>
        public Task ForbidAsync(AuthenticationProperties properties)
        {
            _context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
            return Task.CompletedTask;
        }
 
        /// <summary>
        /// 未登录时的处理
        /// </summary>
        public Task ChallengeAsync(AuthenticationProperties properties)
        {
            _context.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
            return Task.CompletedTask;
        }
    }
}