Пример #1
0
        public static string CreateTokenStr(string Iss, string role, string usr, long overTime)
        {
            long   time = TimeHelper.GetTimeSecond();
            string str  = Iss + "." + role + "." + usr + "." + time + "." + (time + overTime) + "." + Guid.NewGuid().ToString();

            return(str);
        }
Пример #2
0
        /// <summary>
        /// 生成一个token结构
        /// </summary>
        /// <param name="loginName"></param>
        /// <param name="sysid"></param>
        /// <returns></returns>
        private static TokenClaims GetTokenClaims(string iss, string usr, string role, long overTime)
        {
            long time = TimeHelper.GetTimeSecond();

            return(new TokenClaims()
            {
                Usr = usr,
                Iat = time,
                Iss = iss,
                Role = role,
                SingleStr = Guid.NewGuid().ToString(),
                Exp = time + overTime
            });
        }
Пример #3
0
        /// <summary>
        /// 获取请求token需要传递的参数(时间戳+请求身份标识10位+guid)
        /// </summary>
        /// <param name="auth">用户身份标识</param>
        /// <param name="PublicKey">密钥,若不传入</param>
        /// <returns></returns>
        public string GetRequestParam(string auth, string PublicKey)
        {
            string rdStr = Guid.NewGuid().ToString();//new Random().Next(100, 999).ToString();//

            if (PublicKey == null)
            {
                throw new Exception("没有配置publickey");
            }
            else
            {
                string encData = RSAHelper.Encrypt(TimeHelper.GetTimeSecond() + auth + rdStr, PublicKey);
                return(JsonConvert.SerializeObject(new { RequestAuth = encData }));
            }
        }
Пример #4
0
        /// <summary>
        /// 核对缓存中是否存在token以及token的有效性
        /// </summary>
        /// <param name="cachename">缓存名称</param>
        private string CheckCacheToken()
        {
            TokenCacheModel token = TokenCache.GetCacheToken(CacheName);

            if (token != null)
            {
                if (TimeHelper.GetTimeSecond() >= (token.TokenClaim.Exp - 60))
                {
                    return(null);
                }
                else
                {
                    return(token.TokenStr);
                }
            }
            return(null);
        }
Пример #5
0
 /// <summary>
 /// 判断token是否失效
 /// </summary>
 /// <returns></returns>
 private static bool TokenIsTimeLoss(long exptime)
 {
     return(TimeHelper.GetTimeSecond() > exptime);
 }
Пример #6
0
        /// <summary>
        /// 为请求用户生成token
        /// </summary>
        /// <param name="RequestParam">action的参数</param>
        /// <returns></returns>
        public static TokenResult MakeToken(string RequestParam, string PrimaryKey = null)
        {
            try
            {
                dynamic p           = JsonConvert.DeserializeObject(RequestParam);
                string  RequestAuth = p.RequestAuth; //请求人信息
                string  DesAuth;                     //解密后的author
                if (PrimaryKey == null)
                {
                    DesAuth = RSAHelper.Decrypt(RequestAuth, Config_PrimaryKey);
                }
                else
                {
                    DesAuth = RSAHelper.Decrypt(RequestAuth, PrimaryKey);
                }

                #region 请求历史是否有重复
                if (MakeTokenParamHistory.Contains(DesAuth))
                {
                    ToolFactory.LogHelper.Info("生成token身份验证失败:该请求的字符串与之前重复:" + DesAuth);
                    return(new TokenResult()
                    {
                        Success = false, Error_Message = "请求数据非法"
                    });
                }
                MakeTokenParamHistory.Insert(0, DesAuth);
                if (MakeTokenParamHistory.Count > 1000)
                {
                    MakeTokenParamHistory.RemoveRange(1000, MakeTokenParamHistory.Count - 1000);
                }
                #endregion

                string ReqAuthId   = DesAuth.Substring(DesAuth.Length - 46, 10);            //请求人身份标识
                long   reqTimespan = long.Parse(DesAuth.Substring(0, DesAuth.Length - 46)); //客户端请求时间秒数

                if (!ValidTokenAuth(ReqAuthId))
                {
                    ToolFactory.LogHelper.Info("生成token身份验证失败:DesAuth" + DesAuth);
                    return(new TokenResult()
                    {
                        Success = false, Error_Message = "身份验证失败"
                    });
                }

                if ((TimeHelper.GetTimeSecond() - reqTimespan) > ReqToken_OverTime)
                {
                    ToolFactory.LogHelper.Info("生成token请求时间超时:DesAuth" + DesAuth);
                    return(new TokenResult()
                    {
                        Success = false, Error_Message = "请求时间超时"
                    });
                }
                string uname         = TokenBuilder.CreateUserName(ReqAuthId);
                long   TokenOverTime = Token_OverTime;
                if (AuthMapOverTime != null && AuthMapOverTime.ContainsKey(ReqAuthId))
                {
                    TokenOverTime = AuthMapOverTime[ReqAuthId];
                }
                string tokenStr = TokenBuilder.MakeToken(Iss, uname, ReqAuthId, TokenOverTime);
                ToolFactory.LogHelper.Notice("生成token:" + tokenStr);
                return(new TokenResult()
                {
                    Success = true, Token = tokenStr
                });;
            }
            catch (Exception ex)
            {
                ToolFactory.LogHelper.Error("生成token出现异常", ex);
                return(new TokenResult()
                {
                    Success = false, Error_Message = "错误的请求:" + ex.Message
                });
            }
        }