/// <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 })); } }
/// <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 }); } }