/// <summary> /// 判断是否为被挤掉的用户 /// </summary> /// <param name="token">redis Key</param> /// <param name="deleteCrowdedToken">是否删除被挤掉的token数据</param> public bool IsOtherWhereLogin(string token, bool deleteCrowdedToken = false) { bool isCrowed = false; Guid tokenGuid; if (Guid.TryParse(token, out tokenGuid)) { string key = CrowdedTokenKey + ":" + token; isCrowed = !string.IsNullOrEmpty(RedisEntity.ItemGet <string>(CrowdedTokenKey + ":" + token)); if (isCrowed && deleteCrowdedToken) { RedisEntity.ItemSetExpire(key, DateTime.Now.AddMinutes(5)); } } return(isCrowed); }
/// <summary> /// 获取新的用户token /// </summary> /// <param name="val">用户信息</param> /// <returns></returns> public TokenOpearteResult GetNewToken(CustomerInfo val) { string customerName = val.CustomerName; var oldtoken = RedisEntity.HashGet(LoginCustomerNameListKey, customerName); //被挤掉的用户token if (!string.IsNullOrEmpty(oldtoken)) { var customerId = RedisEntity.HashGet(LoginCustomerListKey, oldtoken); CustomerRedisDal cusRedisDal = new CustomerRedisDal(); CustomerDetail oldLoginInfo = cusRedisDal.GetCustomerDetail(long.Parse(customerId)); //不是同一台设备 if (oldLoginInfo != null) { //添加到被挤掉的用户 string crowKey = CrowdedTokenKey + ":" + oldtoken; RedisEntity.ItemSet <string>(crowKey, customerName); RedisEntity.ItemSetExpire(crowKey, DateTime.Now.AddDays(1)); } RedisEntity.HashRemove(LoginCustomerNameListKey, customerName); RedisEntity.HashRemove(LoginCustomerListKey, oldtoken); //记录删除的token列表 LogHelper.Debug("移除旧的token:" + oldtoken + "|" + customerName); } string token = Guid.NewGuid().ToString(); RedisEntity.HashSet(LoginCustomerListKey, token, val.CustomerId.ToString()); RedisEntity.HashSet(LoginCustomerNameListKey, customerName, token); TokenOpearteResult result = new TokenOpearteResult { isok = true, token = token }; return(result); }