LocalAuth.cs
5.4 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using Hh.Mes.Common.Redis;
using WebRepository;
namespace WebApp
{
/// <summary>
/// 使用本地登录。这个注入IAuth时,只需要Mvc一个项目即可,无需webapi的支持
/// </summary>
public class LocalAuth : IAuth
{
private IHttpContextAccessor _httpContextAccessor;
private AuthContextFactory _app;
private LoginParse _loginParse;
//private ICacheContext _cacheContext; , ICacheContext cacheContext
private IUnitWork _unitWork;
private RedisBase redisBase{ get; set; }
public LocalAuth(IHttpContextAccessor httpContextAccessor
, AuthContextFactory app
, LoginParse loginParse
, IUnitWork unitWork)
{
_httpContextAccessor = httpContextAccessor;
_app = app;
_loginParse = loginParse;
//_cacheContext = cacheContext;
_unitWork = unitWork;
redisBase = new RedisBase();
}
private string GetToken()
{
string token = _httpContextAccessor.HttpContext.Request.Query["Token"];
if (!String.IsNullOrEmpty(token)) return token;
var cookie = _httpContextAccessor.HttpContext.Request.Cookies["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;
}
try
{
//var result = _cacheContext.Get<UserAuthSession>(token) != null;
var result = redisBase.GetT<UserAuthSession>(token) !=null;
try
{
//if (result)
//{
// DateTime dateTime = DateTime.Now;
// _unitWork.Update<SysUserOnline>(u => u.Token.Equals(token), u => new SysUserOnline { LastAccessTime = dateTime });
//}
//else
//{
// _unitWork.Delete<SysUserOnline>(u => u.Token.Equals(token));
//}
}
catch (Exception)
{
}
return result;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 获取当前登录的用户信息
/// <para>通过URL中的Token参数或Cookie中的Token</para>
/// </summary>
/// <returns>LoginUserVM.</returns>
public AuthStrategyContext GetCurrentUser()
{
AuthStrategyContext context = null;
// var user = _cacheContext.Get<UserAuthSession>(GetToken());
var user = redisBase.GetT<UserAuthSession>(GetToken());
if (user != null)
{
context = _app.GetAuthStrategyContext(user.Account);
}
return context;
}
/// <summary>
/// 获取当前登录的账户和用户名
/// <para>通过URL中的Token参数或Cookie中的Token</para>
/// </summary>
/// <returns>System.String.</returns>
public List<string> GetUserAccountName()
{
// var user = _cacheContext.Get<UserAuthSession>(GetToken());
var user = redisBase.GetT<UserAuthSession>(GetToken());
if (user != null)
{
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;
try
{
// _unitWork.Delete<SysUserOnline>(u => u.Token.Equals(token));
}
catch (Exception)
{
}
try
{
new RedisBase().redisClient.Del(token);
return true;
}
catch (Exception ex)
{
throw ex;
}
}
}
}