LocalAuth.cs 4.29 KB
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,
            });
        }
    }
}