LocalAuth.cs
4.32 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
142
using Hh.Mes.Pojo.System;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Caching.Distributed;
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 readonly IDistributedCache _cache;
//private ICacheContext _cacheContext; , ICacheContext cacheContext
public LocalAuth(IHttpContextAccessor httpContextAccessor, AuthContextFactory app, LoginParse loginParse, IDistributedCache cache)
{
_httpContextAccessor = httpContextAccessor;
_app = app;
_loginParse = loginParse;
_cache = cache;
}
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];
if (cookie == null)
{
cookie = _httpContextAccessor.HttpContext.Request.Headers["access-token"];
}
return cookie ?? String.Empty;
}
public bool CheckLogin(string token = "")
{
if (string.IsNullOrEmpty(token))
{
token = GetToken();
}
if (string.IsNullOrEmpty(token))
{
return false;
}
var userAuthSessionBytes = _cache.Get(token);
return userAuthSessionBytes != null;
}
/// <summary>
/// 获取当前登录的用户信息
/// <para>通过URL中的Token参数或Cookie中的Token</para>
/// </summary>
/// <returns>LoginUserVM.</returns>
public AuthStrategyContext GetCurrentUser()
{
AuthStrategyContext context = null;
var key = GetToken();
var userBytes = _cache.Get(key);
if (userBytes != null)
{
var user = JsonSerializer.Deserialize<UserAuthSession>(userBytes);
context = _app.GetAuthStrategyContext(user.Account);
}
return context;
}
/// <summary>
/// 获取当前登录的账户和用户名
/// <para>通过URL中的Token参数或Cookie中的Token</para>
/// </summary>
/// <returns>System.String.</returns>
public List<string> GetUserAccountName()
{
var key = GetToken();
var userBytes = _cache.Get(key);
if (userBytes != null)
{
var user = JsonSerializer.Deserialize<UserAuthSession>(userBytes);
return new List<string> { user.Account, user.Name };
}
return null;
}
/// <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,
});
}
/// <summary>
/// 注销
/// </summary>
public bool Logout()
{
var token = GetToken();
if (String.IsNullOrEmpty(token))
{
return true;
}
_cache.Remove(token);
return true;
}
}
}