/// <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; } }
/// <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; }
private StatusCode DelMemberToken(string pMemberId) { var result = new StatusCode(); try { bool isDelete = RedisHelper.KeyDelete(string.Format(RedisConstant.REDIS_KEY_MEMBER_ACCESS_TOKEN, pMemberId)); result.Code = (int)VerifyStatus.Success; result.Message = isDelete.ToString(); 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; } }
private StatusCode DelVerifyInfoWithMemberId(string pMemberId) { var result = new StatusCode(); try { string token = RedisHelper.GetString(string.Format(RedisConstant.REDIS_KEY_MEMBER_ACCESS_TOKEN, pMemberId)); if (string.IsNullOrEmpty(token)) { result.Code = (int)VerifyStatus.Success; return result; } //删除memberToken StatusCode delMemberTokenStatus = DelMemberToken(pMemberId); if (delMemberTokenStatus.Code != (int)VerifyStatus.Success) return delMemberTokenStatus; //删除verifyInfo StatusCode delVerifyStatus = DelVerifyInfo(token); if (delVerifyStatus.Code != (int)VerifyStatus.Success) return delVerifyStatus; result.Code = (int)VerifyStatus.Success; result.Message = (delMemberTokenStatus.Code * delVerifyStatus.Code == 1).ToString(); 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; } }
/// <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; }