// // GET: /Account/WeChatAuth public ActionResult WeChatAuth(string code, string state, string returnUrl) { if (!string.IsNullOrEmpty(code) && !string.IsNullOrEmpty(state) && state.Equals("ShsictReservation")) { // 获取微信授权access_token var client = new WeChatAuthClient(); var result = client.GetUserInfo(code); if (!string.IsNullOrEmpty(result)) { var json = JToken.Parse(result); if (json["UserId"] != null && json["DeviceId"] != null) { // 企业成员授权时返回 {"UserId":"cyrano","DeviceId":"3cc38f93c7d87eec0103c06feca4779f"} var userid = json["UserId"].Value <string>(); var deviceId = json["DeviceId"].Value <string>(); // 授权当前企业号成员 var auth = new AuthorizeManager(); if (auth.AuthorizeUser(userid, deviceId)) { // 设置Cookie FormsAuthentication.SetAuthCookie(userid, true); // 跳转安全检查页面 if (returnUrl.ToLower().Contains("securenode")) { if (Url.IsLocalUrl(returnUrl)) { return(Redirect(returnUrl)); } } // 授权成功跳转订餐界面 return(RedirectToAction("Index", "Reservation")); } } else if (json["OpenId"] != null && json["DeviceId"] != null) { // 非企业成员授权时返回 var openId = json["OpenId"].Value <string>(); var deviceId = json["DeviceId"].Value <string>(); // 授权非企业号成员的关注者 var auth = new AuthorizeManager(); if (auth.AuthorizeGuest(openId, deviceId)) { // 设置Cookie FormsAuthentication.SetAuthCookie(openId, true); // 授权成功跳转用户信息页(补充订餐必要信息) return(RedirectToAction("Index", "Account")); } } // 其他情况下均跳转登录界面 } } // 授权失败跳转登录界面 return(RedirectToAction("Login", "Account", new { weChatRedirect = false })); }