Ejemplo n.º 1
0
        /// <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);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 输出Token验证数据
        /// </summary>
        /// <param name="userid">用户ID</param>
        /// <param name="platform">平台(详见 NH.Entity.EnumLibrary.Regplatform 枚举)</param>
        public void GetTokenVerify(int userid, NH.Entity.EnumLibrary.Regplatform platform)
        {
            if (userid <= 0)
            {
                RequestHelper.RenderJson(new
                {
                    status  = -1,
                    message = "没有登录"
                });
            }
            else
            {
                string token     = GetToken(userid, platform);                    //用户ID+密钥,如: 18-sblvs6hl6nrlch8a2k96ubv663e129s6
                string timestamp = DataConvert.ConvertDateTimeToInt().ToString(); //时间戳
                string noncestr  = GetNonceStr();                                 //16位随机字符串(数字+字母)
                string signature = GetSignature(token, timestamp, noncestr);      //签名
                RequestHelper.RenderJson(new
                {
                    status    = 1,
                    token     = token,
                    timestamp = timestamp,
                    noncestr  = noncestr,
                    signature = signature,
                    message   = "SUCCESS"
                });
                //输入格式如下:

                /*username
                 * {
                 *  token = "18-截止时间-sblvs6hl6nrlch8a2k96ubv663e129s6", //转成base64
                 *  timestamp = 1439880190210,
                 *  noncestr = "sblvs6hl6nrlch8a",
                 *  signature = "sblvs6hl6nrlch8a2k96ubv663e129s6",
                 * }
                 */
            }
        }
Ejemplo n.º 3
0
        /// <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);
        }