/// <summary> /// 通过token 获取用户信息 /// </summary> /// <param name="token">token</param> /// <param name="user"></param> /// <param name="isOtherWhereLogin"></param> /// <returns></returns> public bool GetUserByToken(string token, out CustomerDetail user, out bool isOtherWhereLogin) { bool isok = false; isOtherWhereLogin = false; user = null; //是否为被挤掉的用户 if (!string.IsNullOrEmpty(RedisEntity.ItemGet <string>(CrowdedTokenKey + ":" + token))) { isOtherWhereLogin = true; } else { var customerId = RedisEntity.HashGet(LoginCustomerListKey, token); if (!string.IsNullOrEmpty(customerId)) { //获取用户信息 CustomerRedisDal cusRedisDal = new CustomerRedisDal(); CustomerDetail oldLoginInfo = cusRedisDal.GetCustomerDetail(long.Parse(customerId)); user = oldLoginInfo; } isok = true; } return(isok); }
/// <summary> /// 添加用户 /// </summary> /// <param name="customer">用户信息</param> public void AddCustomerInfo(CustomerDetail customer) { //添加到Redis中 string customerId = customer.CustomerId.ToString(); string userHashId = string.Format(RedisKeyConstants.CustomerHashId, customerId); RedisEntity.HashSetRange <string>(userHashId, JsonHelper.ObjectToDictionary(customer)); }
/// <summary> /// 更新token信息 /// </summary> /// <param name="customerName">用户账号</param> /// <param name="customerId">用户编号</param> public void UpdateTokenUser(string customerName, long customerId) { string token = RedisEntity.HashGet(LoginCustomerNameListKey, customerName); if (!string.IsNullOrEmpty(token)) { RedisEntity.HashSet(LoginCustomerListKey, token, customerId.ToString()); } }
/// <summary> /// 根据key获取openid /// </summary> /// <param name="token"></param> /// <returns></returns> public string OpenId(string token) { string userHashId = string.Format(RedisKeyConstants.WXLoginTokenList); var temp = RedisEntity.HashGet(userHashId, token); string data = null; if (string.IsNullOrEmpty(temp)) { ThrowResponseContextException(ErrCode.TokenPastDue); } else { data = RedisEntity.HashGet(userHashId, token).Split(';')[0]; } return(data); }
/// <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> /// 退出 /// </summary> /// <param name="token">token</param> /// <returns></returns> public TokenOpearteResult TokenLogOff(string token) { var customerId = RedisEntity.HashGet(LoginCustomerListKey, token); string userHashId = string.Format(RedisKeyConstants.CustomerHashId, customerId); string customerName = RedisEntity.HashGet(userHashId, nameof(CustomerDetail.CustomerName)); if (!string.IsNullOrEmpty(customerName)) { RedisEntity.HashRemove(LoginCustomerNameListKey, customerName); } RedisEntity.HashRemove(LoginCustomerListKey, token); TokenOpearteResult result = new TokenOpearteResult { isok = true, result = "退出成功" }; return(result); }
/// <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); }
/// <summary> /// 获取新的用户token /// </summary> /// <param name="val">用户信息</param> /// <returns></returns> public TokenOpearteResult GetWXToken(WxLoginInfo val) { TimeSpan Expire = TimeSpan.FromDays(3); //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(WXLoginListKey, token, val.openid + ";" + val.session_key); RedisEntity.HashSetExpire(token, Expire); TokenOpearteResult result = new TokenOpearteResult { isok = true, token = token }; return(result); }
public override void ProcessRequest(HttpContext param_context) { Stopwatch watch = new Stopwatch(); watch.Start(); guid = Guid.NewGuid().ToString().Replace("-", ""); BaseService.WriteLogWeixin("---------------------------开始接收微信平台推送消息---------------------------|" + guid); string redUrl = ""; string host = ""; string redisKey = ""; try { host = ConfigurationManager.AppSettings["RedisApiUrl"]; var httpContext = param_context; string url = HttpContext.Current.Request.Url.ToString(); BaseService.WriteLogWeixin(" 请求的url:" + url); BaseService.WriteLogWeixin(" 请求方式:" + httpContext.Request.HttpMethod.ToLower()); //把HTTP请求转换为字符串 string postStr = new BaseService().ConvertHttpContextToString(httpContext); //获取post传过来的信息 BaseService.WriteLogWeixin(" 解密前postStr:" + postStr); //延迟几秒执行,看看微信会不会重复推送三次信息 //System.Threading.Thread.Sleep(10000); // //避免微信服务器发起重试 // httpContext.Response.Write(""); // httpContext.Response.Write("success"); //// 延迟几秒执行,看看微信会不会重复推送三次信息(放在后面查看还会不会重试) // System.Threading.Thread.Sleep(10000); //增加redis验证防止多次回调处理脏数据 redisKey = Utils.GetEncryptKey(postStr); var data = string.Empty; var redisEntity = new RedisEntity(); try { //缓存模式 redUrl = host + "keyvalue/get/" + redisKey; //判断缓存中是否存在标示 data = CommonBLL.GetRemoteData(redUrl, "Get", string.Empty); string msg1 = string.Format("url:{0},data:{1}", redUrl, data); BaseService.WriteLogWeixin(msg1 + "|" + guid); redisEntity = data.DeserializeJSONTo <RedisEntity>(); } catch (Exception e) { BaseService.WriteLogWeixin("获取redis参数异常;" + e.Message + "|" + guid); //防止redis服务停止,终止程序 } if (redisEntity.Message == "success") { BaseService.WriteLogWeixin("-------------FSR WEI XIN redis 处理中" + redUrl + ":-------|" + guid); return; } redUrl = host + "keyvalue/set/" + redisKey + "/ScanCode/2"; RedisMedthodAsy(redUrl); if (httpContext.Request.HttpMethod.ToLower() == "post") { if (!string.IsNullOrEmpty(postStr)) { //获取微信公众号信息 LoggingSessionInfo loggingSessionInfo = null; //这里取过之后,后面不用重复取 WApplicationInterfaceEntity wAppEntity = new CommonBLL().GetWAppEntity(postStr, out loggingSessionInfo); string token = string.IsNullOrEmpty(wAppEntity.OpenOAuthAppid) ? wAppEntity.Token : wAppEntity.OpenToken; //如果开放授权给开放平台了就用开放平台的token if (wAppEntity == null || string.IsNullOrEmpty(token)) { return; } if (!string.IsNullOrEmpty(httpContext.Request["echoStr"])) { //用于进行微信平台token验证 new CommonBLL().ValidToken(httpContext, token);//Config.TOKEN配置的token,其实应该是每个客户都有自己的token,配置在数据库里,然后取出来 } #region //验证这个之后看看是否可以去重 //避免微信服务器发起重试 //httpContext.Response.Write(""); //httpContext.Response.Write("success"); // 延迟几秒执行,看看微信会不会重复推送三次信息(放在后面查看还会不会重试) // System.Threading.Thread.Sleep(10000); //在这里要进行加解密,用接收过来信息的ToUserName(公众号的为weixinid,例如: gh_9cbe4cd7941a) int ret = 0; //解密情况 string TrueEncodingAESKey = ""; //如果是安全模式,后面回复信息时,加密的key // int EncryptType = 0; //string appid = ""; //Config.TOKEN 替换为wAppEntity.Token postStr = new CommonBLL().WXDecryptMsg(httpContext, postStr, wAppEntity, loggingSessionInfo, out ret, out TrueEncodingAESKey); // BaseService.WriteLogWeixin(" 解密后post string:" + postStr); if (ret != 0) { BaseService.WriteLogWeixin("解密出现错误 ret: " + ret + "|" + guid); return; } //设置请求参数 var requestParams = SetRequestParams(postStr, TrueEncodingAESKey, httpContext, loggingSessionInfo, wAppEntity); BaseService.WriteLogWeixin("请求参数:" + requestParams.ToJSON() + "|" + guid); //响应微信平台推送消息 ResponseMsg(httpContext, requestParams); #endregion } } else//专门用于微信公众号里“填写服务器配置”,这时候向这个url传递的方式是get,没有weixinid,无法从数据库里取数据 { if (!string.IsNullOrEmpty(httpContext.Request["echoStr"])) { //用于进行微信平台token验证 new CommonBLL().ValidToken(httpContext, JIT.CPOS.BS.BLL.WX.Const.Config.TOKEN);//Config.TOKEN配置的token,其实应该是。每个客户都有自己的token,配置在数据库里,然后取出来 } } redUrl = host + "keyvalue/del/" + redisKey; //数据处理成功,删除缓存 RedisMedthodAsy(redUrl); } catch (Exception ex) { redUrl = host + "keyvalue/del/" + redisKey; //数据处理成功,删除缓存 string msg = string.Format("异常信息:【{0}】{1}", redUrl, ex.Message + "|" + guid); BaseService.WriteLogWeixin(msg); try { CommonBLL.GetRemoteData(redUrl, "Get", string.Empty); } catch (Exception ex1) { msg = string.Format("异常信息2:【{0}】{1}", redUrl, ex1.Message + "|" + guid); BaseService.WriteLogWeixin(msg); } } watch.Stop(); BaseService.WriteLogWeixin("-------------FSR WEI XIN ex1接收微信平台推送消息处理结束:" + watch.ElapsedMilliseconds + "|" + guid); }
public IndexEntity(RedisEntity entity, string indexValue) { Id = indexValue; EntityId = entity.Id; }
/// <summary> /// 判断token是否存在 /// </summary> /// <param name="token">token</param> public bool IsTokenExist(string token) { return(!string.IsNullOrEmpty(RedisEntity.HashGet(LoginCustomerListKey, token))); }
/// <summary> /// 获取token对应的CustomerID /// </summary> /// <param name="token"></param> /// <returns></returns> public string GetCustomerIDByToken(string token) { return(RedisEntity.HashGet(LoginCustomerListKey, token)); }
public string WXGetUserByToken(string token) { var openId = RedisEntity.HashGet(WXLoginListKey, token); return(openId); }
/// <summary> /// 判断token是否存在 /// </summary> /// <param name="token">token</param> public bool IsWXTokenExist(string token) { return(!string.IsNullOrEmpty(RedisEntity.HashGet(WXLoginListKey, token))); }