// // GET: /Account/WeChatAuth public ActionResult WeChatAuth(string code, string state) { // 获取微信授权access_token var client = new WeChatSnsClient(); var result = client.GetAccessToken(code); if (!string.IsNullOrEmpty(result)) { var json = JToken.Parse(result); if (json["access_token"] != null && json["expires_in"] != null && json["refresh_token"] != null && json["openid"] != null && json["scope"] != null) { var scope = (ScopeType)Enum.Parse(typeof(ScopeType), json["scope"].Value<string>()); var accessToken = json["access_token"].Value<string>(); var openId = json["openid"].Value<string>(); UserWeChatDto.Authorize(new Guid(state), accessToken, json["expires_in"].Value<double>(), json["refresh_token"].Value<string>(), openId, scope); } } TempData["DataUrl"] = "data-url=/"; return RedirectToAction("Index", "Home"); }
// // GET: /Account/WeChatLogin public ActionResult WeChatLogin(ScopeType scope) { var user = UserDto.GetSession(); if (HttpContext.Request.Url != null) { var client = new WeChatSnsClient(); var openUri = client.GetOpenUrl($"http://{HttpContext.Request.Url.Host}/Account/WeChatAuth", scope, user.ID.ToString()); if (!string.IsNullOrEmpty(openUri)) { TempData["DataUrl"] = $"data-url={openUri}"; return Redirect(openUri); } } TempData["DataUrl"] = "data-url=/Account"; return RedirectToAction("Index", "Account"); }
public static UserWeChat Authorize(Guid userGuid, string accessToken, double expiresIn, string refreshToken, string openId, ScopeType scope, bool anonymous = false) { using (var conn = new SqlConnection(DataAccess.ConnectString)) { conn.Open(); var trans = conn.BeginTransaction(); try { IRepository repo = new Repository(); // 保存微信用户 var user = anonymous ? repo.Single<User>(userGuid) : UserDto.GetSession(); if (user != null && user.ID == userGuid) { var u = new UserWeChat(); if (repo.Any<UserWeChat>(userGuid)) { u = repo.Single<UserWeChat>(userGuid); } u.ID = userGuid; u.UserName = user.UserName; u.LastAuthorizeDate = DateTime.Now; u.AccessToken = accessToken; u.AccessTokenExpiredDate = DateTime.Now.AddSeconds(expiresIn); u.RefreshToken = refreshToken; u.RefreshTokenExpiredDate = DateTime.Now.AddDays(30); u.Gender = 0; if (u.Province == null) u.Province = string.Empty; if (u.City == null) u.City = string.Empty; if (u.Country == null) u.Country = string.Empty; if (u.HeadImgUrl == null) u.HeadImgUrl = string.Empty; if (u.Privilege == null) u.Privilege = string.Empty; if (u.UnionID == null) u.UnionID = string.Empty; repo.Save(u, trans); // 更新普通用户 user.WeChatOpenID = openId; // 按scope,获取微信用户详情 if (scope.Equals(ScopeType.snsapi_userinfo)) { var result = new WeChatSnsClient().GetUserInfo(accessToken, openId); if (!string.IsNullOrEmpty(result)) { var json = JToken.Parse(result); user.WeChatNickName = json["nickname"].Value<string>(); u.Gender = json["sex"].Value<short>(); u.Province = json["province"].Value<string>(); u.City = json["city"].Value<string>(); u.Country = json["country"].Value<string>(); u.HeadImgUrl = json["headimgurl"].Value<string>(); u.Privilege = json["privilege"].Value<JArray>().ToString(); u.UnionID = json["unionid"] != null ? json["unionid"].Value<string>() : string.Empty; repo.Update(u, trans); } } // 更新user的openId, nickname repo.Update(user, trans); trans.Commit(); return u; } return null; } catch { trans.Rollback(); throw; } } }