public void init() { service = new VerifyService(); int validSeconds = 20; var m = new Member() { MemberId = "123456", //Vno = "V654321", //MemberLevel = "I", Mobile = "1366155555", Name = "washing", Point = 9999 }; vInfo = new VerifyInfo(); vInfo.MemberId = m.MemberId; vInfo.Caller = "Web"; vInfo.RefreshToken = string.Empty; vInfo.ValidDate = DateTime.Now.AddSeconds(validSeconds); vInfo.ValidMinutes = 1; vInfo.VerifiedMember = m; }
public void Redis_Set() { int validSeconds = 20; for (int i = 0; i < 10; i++) { Member m = new Member() { MemberId = "123456", //Vno = "V654321", //MemberLevel = "I", Mobile = "1366155555", Name = "washing", Point = 9999 }; var vInfo = new VerifyInfo(); //vInfo.AccessToken = Guid.NewGuid().ToString(); vInfo.AccessToken = i.ToString(); vInfo.Caller = "Web"; vInfo.RefreshToken = string.Empty; vInfo.ValidDate = DateTime.Now.AddSeconds(validSeconds); vInfo.VerifiedMember = m; RedisHelper.SetString(string.Format(RedisConstant.REDIS_KEY_ACCESS_TOKEN, vInfo.AccessToken), JsonConvert.SerializeObject(vInfo), validSeconds); } }
private void GetMemberInfo(string pToken) { IVerifyService service = new VerifyService(); var verifyInfo = new VerifyInfo(); StatusCode statusCode = service.GetVerifyInfo(pToken, ref verifyInfo); if (statusCode.Code == 1) { if (verifyInfo == null) TextBox1.Text = "调用成功但未获取到MemberInfo对象"; else TextBox1.Text = JsonConvert.SerializeObject(verifyInfo); } }
/// <summary> /// 根据token获取token相关信息和对应的memberInfo数据 /// </summary> /// <param name="pAccessToken"></param> /// <param name="pVerifyInfo"></param> /// <returns></returns> public StatusCode GetVerifyInfo(string pAccessToken, ref VerifyInfo pVerifyInfo) { var verifyInfo = new VerifyInfo(); var result = new StatusCode(); if (VerifyInfoNotExists(pAccessToken, ref verifyInfo)) { result.Code = (int)VerifyStatus.Failed; result.Message = "无效的Token"; return result; } result.Code = (int)VerifyStatus.Success; return result; }
/// <summary> /// 登陆成功后调用, 删除原用户信息以及token, 重新添加 /// </summary> /// <param name="pVerifyInfo"></param> /// <param name="pLoginDate"></param> /// <returns></returns> public StatusCode SetVerifyInfo(VerifyInfo pVerifyInfo, DateTime pLoginDate) { var result = new StatusCode(); try { //生成签名 var signData = string.Concat(pVerifyInfo.MemberId, pVerifyInfo.PrivateKey, pLoginDate.ToString("yyyy-MM-dd HH:mm:ss")); pVerifyInfo.AccessToken = MD5Encoding(signData); //删除验证信息 StatusCode deleteStatus = DelVerifyInfoWithMemberId(pVerifyInfo.MemberId); if (deleteStatus.Code == (int)VerifyStatus.Success) { if (deleteStatus.Message == Boolean.TrueString) { HZLogger.Trace(string.Format("Token失效前用户重新登陆, MemberId:{0}", pVerifyInfo.MemberId)); } } //重新设置membertoken key-memberid, value-token SetRedisMemberToken(pVerifyInfo); //重新设置token, 并续时 SetRedisVerify(pVerifyInfo); result.Code = (int)VerifyStatus.Success; return result; } catch (RedisConnectionException connExp) { HZLogger.Error(connExp); result.Message = "无法连接至Redis"; return result; } catch (Exception exp) { HZLogger.Error(exp); result.Code = (int)VerifyStatus.Failed; result.Message = "SSO_Exception:" + exp.Message; return result; } }
public bool Execute(string pName, string pPassword, Caller pCaller, DateTime pLoginTime, ref Member pMemberInfo, ref string pErrorMsg, ref DateTime ValidTime, ref string pAccessToken) { if ((DateTime.Now - pLoginTime).TotalMinutes >= 5) { pErrorMsg = "无效请求"; return false; } #region 获取登陆信息 int result = 0; string extCardNo = null; string vno = null; PersonMember person = null; pErrorMsg = string.Empty; if (!CallLoginCheck(pName, pPassword, ref pErrorMsg, ref result, ref person, ref vno, ref extCardNo)) return false; try { pMemberInfo = ConvertToMemberInfo(person, vno); } catch (Exception exp) { HZLogger.Error(exp); pErrorMsg = "用户名或密码无效"; return false; } pMemberInfo.DefaultVCardNo = vno; pMemberInfo.DefaultExtCardNo = extCardNo; if (pMemberInfo.MemberLevelID == "P") pMemberInfo.CompanyMemberType = (int)CompanyMemberType.NoSet; else pMemberInfo.CompanyMemberType = result == 2 ? (int)CompanyMemberType.User : (int)CompanyMemberType.Admin; #endregion #region 缓存认证信息至redis var verifyInfo = new VerifyInfo(); verifyInfo.VerifiedMember = pMemberInfo; verifyInfo.Caller = pCaller.CallerID; verifyInfo.PrivateKey = pCaller.PrivateKey; verifyInfo.RefreshToken = string.Empty; verifyInfo.ValidMinutes = pCaller.UserTokenValidTime; verifyInfo.ValidDate = DateTime.Now.AddSeconds(verifyInfo.ValidMinutes); ValidTime = verifyInfo.ValidDate; StatusCode status = _verifyService.SetVerifyInfo(verifyInfo, pLoginTime); if (status.Code == (int)VerifyStatus.Success) { pAccessToken = verifyInfo.AccessToken; return true; } else { pErrorMsg = status.Message; return false; } #endregion }
public StatusCode ContinueTokenWithRefreshToken(string pRefreshToken, ref VerifyInfo pVerifyInfo) { throw new NotImplementedException(); }
private static bool VerifyInfoNotExists(string pAccessToken, ref VerifyInfo pVerifyInfo) { //验证token是否存在 var verifyInfoString = RedisHelper.GetString(string.Format(RedisConstant.REDIS_KEY_ACCESS_TOKEN, pAccessToken)); if (string.IsNullOrEmpty(verifyInfoString)) { return true; } pVerifyInfo = JsonConvert.DeserializeObject<VerifyInfo>(verifyInfoString); return false; }
private static void SetRedisVerify(VerifyInfo pVerifyInfo) { RedisHelper.SetString(string.Format(RedisConstant.REDIS_KEY_ACCESS_TOKEN, pVerifyInfo.AccessToken), JsonConvert.SerializeObject(pVerifyInfo), pVerifyInfo.ValidMinutes * 60); }
private static void SetRedisMemberToken(VerifyInfo pVerifyInfo) { RedisHelper.SetString(string.Format(RedisConstant.REDIS_KEY_MEMBER_ACCESS_TOKEN, pVerifyInfo.MemberId), pVerifyInfo.AccessToken, pVerifyInfo.ValidMinutes * 60); }
/// <summary> /// 根据token续签token /// </summary>x /// <param name="pAccessToken"></param> /// <param name="pVerifyInfo"></param> /// <returns></returns> public StatusCode ContinueToken(string pAccessToken, ref VerifyInfo pVerifyInfo) { var result = new StatusCode(); try { //获取Token对象 if (VerifyInfoNotExists(pAccessToken, ref pVerifyInfo)) { result.Code = (int)VerifyStatus.TokenNotExists; result.Message = "Token已失效, 请重新登陆"; return result; } pVerifyInfo.ValidDate = DateTime.Now.AddSeconds(pVerifyInfo.ValidMinutes); //重新设置membertoken key-memberid, value-token SetRedisMemberToken(pVerifyInfo); //重新设置token, 并续时 SetRedisVerify(pVerifyInfo); result.Code = (int)VerifyStatus.Success; return result; } catch (RedisConnectionException connExp) { result.Code = (int)VerifyStatus.Failed; result.Message = "无法连接至Redis"; HZLogger.Error(connExp); return result; } catch (Exception exp) { HZLogger.Error(exp); result.Code = (int)VerifyStatus.Failed; result.Message = "SSO_Exception:" + exp.Message; return result; } }
/// <summary> /// 删除token对应的用户信息和token数据 /// </summary> /// <param name="pAccessToken"></param> /// <returns></returns> public StatusCode SignOff(string pAccessToken) { var verifyInfo = new VerifyInfo(); var result = new StatusCode(); if (VerifyInfoNotExists(pAccessToken, ref verifyInfo)) { result.Code = (int)VerifyStatus.Success; return result; } result = DelVerifyInfoWithMemberId(verifyInfo.MemberId); return result; }