LocalAuth.cs
4.29 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using Hh.Mes.Common;
using Hh.Mes.Pojo.System;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Text.Json;
namespace Hh.Mes.Service.SystemAuth
{
/// <summary>
/// 使用本地登录。这个注入IAuth时,只需要Mvc一个项目即可,无需webapi的支持
/// </summary>
public class LocalAuth : IAuth
{
private IHttpContextAccessor _httpContextAccessor;
private AuthContextFactory _app;
private LoginParse _loginParse;
//private ICacheContext _cacheContext; , ICacheContext cacheContext
public LocalAuth(IHttpContextAccessor httpContextAccessor, AuthContextFactory app, LoginParse loginParse)
{
_httpContextAccessor = httpContextAccessor;
_app = app;
_loginParse = loginParse;
}
private string GetToken()
{
string token = _httpContextAccessor.HttpContext.Request.Query[SSOAuthAttribute.token];
if (!String.IsNullOrEmpty(token)) return token;
var cookie = _httpContextAccessor.HttpContext.Request.Cookies[SSOAuthAttribute.token];
cookie ??= _httpContextAccessor.HttpContext.Request.Headers["access-token"];
return cookie ?? String.Empty;
}
public bool CheckLogin(string token = "")
{
try
{
if (string.IsNullOrWhiteSpace(token))
{
token = GetToken();
}
if (string.IsNullOrWhiteSpace(token))
{
return false;
}
JwtEncryption.Decode(token);
return true;
}
catch (Exception)
{
return false;
}
}
/// <summary>
/// 获取当前登录的用户信息
/// <para>通过URL中的Token参数或Cookie中的Token</para>
/// </summary>
/// <returns>LoginUserVM.</returns>
public AuthStrategyContext GetCurrentUser()
{
try
{
var token = GetToken();
if (string.IsNullOrWhiteSpace(token))
{
return null;
}
var json = JwtEncryption.Decode(token);
var user = JsonSerializer.Deserialize<UserAuthSession>(json);
return _app.GetAuthStrategyContext(user.Account);
}
catch (Exception)
{
return null;
}
}
/// <summary>
/// 获取当前登录的账户和用户名
/// <para>通过URL中的Token参数或Cookie中的Token</para>
/// </summary>
/// <returns>System.String.</returns>
public List<string> GetUserAccountName()
{
try
{
var token = GetToken();
if (string.IsNullOrWhiteSpace(token))
{
return new List<string>();
}
var json = JwtEncryption.Decode(token);
var user = JsonSerializer.Deserialize<UserAuthSession>(json);
return new List<string> { user.Account, user.Name };
}
catch (Exception)
{
return new List<string>();
}
}
/// <summary>
/// 获取当前登录的账户和用户名
/// <para>通过URL中的Token参数或Cookie中的Token</para>
/// </summary>
/// <returns>System.String.</returns>
public List<string> GetUserAccountName(string username)
{
var user = _app.GetAuthStrategyContext(username).User;
if (user != null)
{
return new List<string> { user.Account, user.Name };
}
return null;
}
/// <summary>
/// 登录接口
/// </summary>
/// <param name="appKey">应用程序key.</param>
/// <param name="username">用户名</param>
/// <param name="pwd">密码</param>
/// <returns>System.String.</returns>
public LoginResult Login(string appKey, string username, string pwd)
{
return _loginParse.Do(new PassportLoginRequest
{
AppKey = appKey,
Account = username,
Password = pwd,
});
}
}
}