public Tuple <string, UserInfo> OAuthCallback(string code) { if (string.IsNullOrEmpty(code)) { throw new Exception("无效的auth code"); } var accessToken = _authClient.GetAccessToken(code).Result; var qqUser = _authClient.GetUserInfo(accessToken).Result; //未登录,通过此ID获取用户 var userInfo = _userDapper.FindByQQOpenID(qqUser.Id); if (userInfo == null) { //新增用户 _userDapper.InsertUserForQQAuth(new UserInfo() { UserName = qqUser.Name, QQOpenUID = qqUser.Id, AvatarUrl = qqUser.ImgUrl, JsonData = JsonConvert.SerializeObject(qqUser) }); userInfo = _userDapper.FindByQQOpenID(qqUser.Id); } string token = userInfo.NewLoginToken; WriteUserToken(userInfo, token); return(Tuple.Create <string, UserInfo>(token, userInfo)); }
public Tuple <string, UserInfo> OAuthCallback(string code) { if (string.IsNullOrEmpty(code)) { throw new Exception("无效的auth code"); } var accessToken = _authClient.GetAccessToken(code).Result; var qqUser = _authClient.GetUserInfo(accessToken).Result; //未登录,通过此ID获取用户 var userInfo = _context.Users.FirstOrDefault(u => u.QQOpenUID == qqUser.Id); if (userInfo == null) { //新增用户 var user = new UserInfo() { UserName = qqUser.Name, QQOpenUID = qqUser.Id, AvatarUrl = qqUser.ImgUrl, JsonData = JsonConvert.SerializeObject(qqUser) }; _context.Users.Add(user); _context.SaveChanges(); userInfo = user; } string token = userInfo.NewLoginToken; WriteUserToken(userInfo, token); return(Tuple.Create <string, UserInfo>(token, userInfo)); }
public ActionResult Callback(string code, string state) { if (!string.IsNullOrEmpty(code)) { try { var accessToken = _authClient.GetAccessToken(code).Result; var qqUser = _authClient.GetUserInfo(accessToken).Result; //未登录,通过此ID获取用户 var userInfo = _userDataDapper.FindUserByQQOpenUID(qqUser.Id); if (userInfo == null) { //新增用户 _userDataDapper.InsertUserForQQAuth(new UserInfo() { UserName = qqUser.Name, QQOpenUID = qqUser.Id }); userInfo = _userDataDapper.FindUserByQQOpenUID(qqUser.Id); } string token = _encryptionTools.Crypt($"{userInfo.ID}|{userInfo.UserName}"); _userService.WriteUserToken(userInfo, token); return(Ok(new { success = true, token = token, message = "登录成功!", data = userInfo })); } catch (Exception ex) { return(Ok(new { success = false, error = ex.ToString() })); } } return(Ok(new { success = false, error = "无效的auth code" })); }
public ActionResult Auth(string code, string state) { if (!string.IsNullOrEmpty(code)) { try { var accessToken = authClient.GetAccessToken(code).Result; var qqUser = authClient.GetUserInfo(accessToken).Result; var userInfo = userDataDapper.FindByID(GetUserID()); //已登录 = 绑定此QQ if (userInfo != null) { userInfo.QQOpenUID = qqUser.Id; userDataDapper.UpdateUser(userInfo, new List <string>() { "QQOpenUID" }); return(View(new ViewResult() { success = true, message = "绑定成功!" })); } //未登录,通过此ID获取用户 userInfo = userDataDapper.FindUserByQQOpenUID(qqUser.Id); if (userInfo == null) { //新增用户 userDataDapper.InsertUserForQQAuth(new UserInfo() { UserName = qqUser.Name, QQOpenUID = qqUser.Id }); userInfo = userDataDapper.FindUserByQQOpenUID(qqUser.Id); } var user = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, userInfo.UserName), new Claim(ClaimTypes.Email, userInfo.Email), new Claim(ClaimTypes.NameIdentifier, userInfo.ID.ToString()) }, CookieAuthenticationDefaults.AuthenticationScheme)); HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, user, new AuthenticationProperties { IsPersistent = true, ExpiresUtc = DateTimeOffset.Now.Add(TimeSpan.FromDays(7)) // 有效时间 }).Wait(); return(View(new ViewResult() { success = true, message = "登录成功!" })); } catch (Exception ex) { return(View(new ViewResult() { success = false, error = ex.ToString() })); } } return(View(new ViewResult() { success = false, error = "无效的auth code" })); }