private async Task <IdentityResult> CreateOrUpdateUserLogin(User user, UserLoginInfoEx loginInfoEx) { if (string.IsNullOrEmpty(user.HeadImg) && !string.IsNullOrEmpty(loginInfoEx.AvatarUrl)) { user.HeadImg = loginInfoEx.AvatarUrl; } UserLogin userLogin = _userLoginRepository.GetFirst(m => m.LoginProvider == loginInfoEx.LoginProvider && m.ProviderKey == loginInfoEx.ProviderKey); if (userLogin == null) { userLogin = new UserLogin() { LoginProvider = loginInfoEx.LoginProvider, ProviderKey = loginInfoEx.ProviderKey, ProviderDisplayName = loginInfoEx.ProviderDisplayName, Avatar = loginInfoEx.AvatarUrl, UserId = user.Id }; await _userLoginRepository.InsertAsync(userLogin); } else { userLogin.UserId = user.Id; await _userLoginRepository.UpdateAsync(userLogin); } return(IdentityResult.Success); }
/// <summary> /// 登录并绑定现有账号 /// </summary> /// <param name="loginInfoEx">第三方登录信息</param> /// <returns>业务操作结果</returns> public virtual async Task <OperationResult <User> > LoginBind(UserLoginInfoEx loginInfoEx) { UserLoginInfoEx existLoginInfoEx = await GetLoginInfoEx(loginInfoEx.ProviderKey); if (existLoginInfoEx == null) { return(new OperationResult <User>(OperationResultType.Error, "无法找到相应的第三方登录信息")); } LoginDto loginDto = new LoginDto() { Account = loginInfoEx.Account, Password = loginInfoEx.Password }; OperationResult <User> loginResult = await Login(loginDto); if (!loginResult.Succeeded) { return(loginResult); } User user = loginResult.Data; IdentityResult result = await CreateOrUpdateUserLogin(user, existLoginInfoEx); if (!result.Succeeded) { return(result.ToOperationResult(user)); } return(new OperationResult <User>(OperationResultType.Success, "登录并绑定账号成功", user)); }
public async Task <ActionResult> OAuth2Callback(string returnUrl = null, string remoteError = null) { if (remoteError != null) { Logger.LogError($"第三方登录错误:{remoteError}"); return(Json(new AjaxResult($"第三方登录错误:{remoteError}", AjaxResultType.UnAuth))); } string url; ExternalLoginInfo info = await _signInManager.GetExternalLoginInfoAsync(); if (info == null) { url = "/#/exception/500"; Logger.LogError("第三方登录返回的用户信息为空"); return(Redirect(url)); } UserLoginInfoEx loginInfo = info.ToUserLoginInfoEx(); var result = await _identityContract.LoginOAuth2(loginInfo); // 登录不成功,将用户信息返回前端,让用户选择绑定现有账号还是创建新账号 if (!result.Succeeded) { string cacheId = (string)result.Data; loginInfo.ProviderKey = cacheId; url = $"/#/passport/oauth-callback?type={loginInfo.LoginProvider}&id={cacheId}&name={loginInfo.ProviderDisplayName?.ToUrlEncode()}&avatar={loginInfo.AvatarUrl?.ToUrlEncode()}"; return(Redirect(url)); } Logger.LogInformation($"用户“{info.Principal.Identity.Name}”通过 {info.ProviderDisplayName} OAuth2登录成功"); string token = await CreateJwtToken((User)result.Data); url = $"/#/passport/oauth-callback?token={token}"; return(Redirect(url)); }
private async Task <string> SetLoginInfoEx(UserLoginInfoEx loginInfo) { string cacheId = Guid.NewGuid().ToString("N"); string key = $"Identity_UserLoginInfoEx_{cacheId}"; await _cache.SetAsync(key, loginInfo, 60 * 5); return(cacheId); }
/// <summary> /// 获取第三方登录信息 /// </summary> public static UserLoginInfoEx ToUserLoginInfoEx(this ExternalLoginInfo loginInfo) { if (!(loginInfo.Principal.Identity is ClaimsIdentity identity)) { return(null); } string displayName = identity.GetUserName(); UserLoginInfoEx info = new UserLoginInfoEx(loginInfo.LoginProvider, loginInfo.ProviderKey, displayName) { AvatarUrl = identity.FindFirst(m => m.Type == "urn:qq:figure")?.Value }; return(info); }
/// <summary> /// 使用第三方用户信息进行OAuth2登录 /// </summary> /// <param name="loginInfo">第三方用户信息</param> /// <returns>业务操作结果</returns> public async Task <OperationResult> LoginOAuth2(UserLoginInfoEx loginInfo) { SignInResult result = await _signInManager.ExternalLoginSignInAsync(loginInfo.LoginProvider, loginInfo.ProviderKey, true); if (!result.Succeeded) { string cacheId = await SetLoginInfoEx(loginInfo); return(new OperationResult(OperationResultType.Error, "登录失败", cacheId)); } User user = await _userManager.FindByLoginAsync(loginInfo.LoginProvider, loginInfo.ProviderKey); return(new OperationResult(OperationResultType.Success, "登录成功", user)); }
/// <summary> /// 一键创建新用户并登录 /// </summary> /// <param name="cacheId">第三方登录信息缓存编号</param> /// <returns>业务操作结果</returns> public virtual async Task <OperationResult <User> > LoginOneKey(string cacheId) { UserLoginInfoEx loginInfoEx = await GetLoginInfoEx(cacheId); if (loginInfoEx == null) { return(new OperationResult <User>(OperationResultType.Error, "无法找到相应的第三方登录信息")); } IdentityResult result; User user = await _userManager.FindByLoginAsync(loginInfoEx.LoginProvider, loginInfoEx.ProviderKey); if (user == null) { user = new User() { UserName = $"{loginInfoEx.LoginProvider}_{loginInfoEx.ProviderKey}", NickName = loginInfoEx.ProviderDisplayName, HeadImg = loginInfoEx.AvatarUrl }; result = await _userManager.CreateAsync(user); if (!result.Succeeded) { return(result.ToOperationResult(user)); } UserDetail detail = new UserDetail() { RegisterIp = loginInfoEx.RegisterIp, UserId = user.Id }; int count = await _userDetailRepository.InsertAsync(detail); if (count == 0) { return(new OperationResult <User>(OperationResultType.NoChanged)); } } result = await CreateOrUpdateUserLogin(user, loginInfoEx); if (!result.Succeeded) { return(result.ToOperationResult(user)); } return(new OperationResult <User>(OperationResultType.Success, "第三方用户一键登录成功", user)); }
public async Task <AjaxResult> LoginBind(UserLoginInfoEx loginInfo) { loginInfo.RegisterIp = HttpContext.GetClientIp(); OperationResult <User> result = await _identityContract.LoginBind(loginInfo); IUnitOfWork unitOfWork = HttpContext.RequestServices.GetUnitOfWork <User, int>(); unitOfWork.Commit(); if (!result.Succeeded) { return(result.ToAjaxResult()); } User user = result.Data; JsonWebToken token = await CreateJwtToken(user); return(new AjaxResult("登录成功", AjaxResultType.Success, token)); }
public async Task <AjaxResult> LoginOneKey(UserLoginInfoEx loginInfo) { loginInfo.RegisterIp = HttpContext.GetClientIp(); IUnitOfWork unitOfWork = HttpContext.RequestServices.GetUnitOfWork(true); OperationResult <User> result = await _identityContract.LoginOneKey(loginInfo.ProviderKey); #if NET5_0 await unitOfWork.CommitAsync(); #else unitOfWork.Commit(); #endif if (!result.Succeeded) { return(result.ToAjaxResult()); } User user = result.Data; JsonWebToken token = await CreateJwtToken(user); return(new AjaxResult("登录成功", AjaxResultType.Success, token)); }