using Hh.Mes.Common.Redis; using Hh.Mes.Pojo.System; using Microsoft.AspNetCore.Http; using System; using System.Collections.Generic; 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 private RedisBase redisBase{ get; set; } public LocalAuth(IHttpContextAccessor httpContextAccessor, AuthContextFactory app, LoginParse loginParse) { _httpContextAccessor = httpContextAccessor; _app = app; _loginParse = loginParse; //_cacheContext = cacheContext; redisBase = new RedisBase(); } 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; } 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; } } } }