/// <summary> /// 根据Token的Base64字符转换成对应的Token对象 /// </summary> /// <param name="tokenBase64"></param> /// <returns></returns> public NH.Entity.Model.Token GetToken(string tokenBase64) { try { if (!Utils.StrIsNullOrEmpty(tokenBase64)) { string tokenString = GetDecrypTokenString(tokenBase64); string[] arrayToken = tokenString.Split(new Char[] { '-' }); if (arrayToken.Length > 0) { string useridBase64 = Convert.ToString(arrayToken[0]); long timeStamp = TypeConverter.StrToInt64(arrayToken[1].ToString(), 0); string appKey = Convert.ToString(arrayToken[2]); bool IsValidSecretKey = GetIsValidSecretKey(appKey); int platform = 0; if (arrayToken.Length > 3) { platform = TypeConverter.StrToInt(arrayToken[3]); } int authenticate = 0; if (arrayToken.Length > 4) { authenticate = TypeConverter.StrToInt(arrayToken[4]); } NH.Entity.Model.Token token = new NH.Entity.Model.Token { UserID = TypeConverter.StrToInt(DataConvert.ConvertBase64ToString(useridBase64), 0), IsOverdue = GetIsOverdue(timeStamp), IsValidSecretKey = IsValidSecretKey, AppKey = appKey, Platform = (NH.Entity.EnumLibrary.Regplatform)platform, Authenticate = authenticate }; return(token); } } } catch { } return(null); }
/// <summary> /// 根据用户ID计算出Base64数据的Token /// </summary> /// <param name="userid">用户ID</param> /// <param name="platform">平台(详见 NH.Entity.EnumLibrary.Regplatform 枚举)</param> /// <returns></returns> public string GetToken(int userid, NH.Entity.EnumLibrary.Regplatform platform) { NH.Entity.Model.Token token = this.GetTokenEntity(userid, platform); return(token.TokenStr); //if (userid <= 0) // return string.Empty; ////密钥(32位数字+字母组成的字符串) //string AppKey = ConfigHelper.GetConfigValueByKey("AppKey"); ////从当前时间开始指定天数后(截止天数) //int AppTokenValidDays = TypeConverter.StrToInt(ConfigHelper.GetConfigValueByKey("AppTokenValidDays"), 7); ////把用户ID转换成Base64字符 //string useridBase64 = DataConvert.ConvertStringToBase64(userid.ToString()); //long timeStamp = DataConvert.ConvertDateTimeToInt(AppTokenValidDays); //int authenticate = 0; //if (platform == NH.Entity.EnumLibrary.Regplatform.MiniCourse) //{ // authenticate = NH.Service.V2.ExpertService.GetAuthenticateStatus(userid); //} //string[] arrayTemp = { useridBase64, timeStamp.ToString(), AppKey, ((int)platform).ToString(), authenticate.ToString() }; //string token = string.Join("-", arrayTemp); //return DataConvert.ConvertStringToBase64(token); }
/// <summary> /// 输出Token实体 /// </summary> /// <param name="userid">用户ID</param> /// <param name="platform">平台(详见 NH.Entity.EnumLibrary.Regplatform 枚举)</param> /// <returns></returns> public NH.Entity.Model.Token GetTokenEntity(int userid, NH.Entity.EnumLibrary.Regplatform platform) { NH.Entity.Model.Token token = new NH.Entity.Model.Token(); if (userid > 0) { //密钥(32位数字+字母组成的字符串) string AppKey = ""; if (ConfigHelper.GetConfigValueByKey("Token_IsEncryptToken").ToInt() != 1) { AppKey = ConfigHelper.GetConfigValueByKey("AppKey"); } //从当前时间开始指定天数后(截止天数) int AppTokenValidDays = TypeConverter.StrToInt(ConfigHelper.GetConfigValueByKey("AppTokenValidDays"), 7); token.UserID = userid; token.Timestamp = DataConvert.ConvertDateTimeToInt(AppTokenValidDays); //if (platform == NH.Entity.EnumLibrary.Regplatform.MiniCourse) //{ // token.Authenticate = NH.Service.V2.ExpertService.GetAuthenticateStatus(userid); //} token.Platform = platform; //把用户ID转换成Base64字符 string useridBase64 = DataConvert.ConvertStringToBase64(token.UserID.ToString()); string[] arrayTemp = new string[] { useridBase64, token.Timestamp.ToString(), AppKey, ((int)token.Platform).ToString() }; string tokenstr = string.Join("-", arrayTemp); token.TokenStr = GetEncrypTokenString(tokenstr); //token.UserID = userid; //token.Timestamp = DataConvert.ConvertDateTimeToInt(); //时间戳 //token.Noncestr = GetNonceStr(); //16位随机字符串(数字+字母) //token.Signature = GetSignature(token.TokenStr, token.Timestamp.ToString(), token.Noncestr); //签名 } return(token); }