public async Task <AjaxResponse> Authenticate(ThirdPartyLoginModel input) { IThirdPartyAuthService authService; switch (input.ThirdParty) { case ThirdParty.QQ: authService = _qqAuthService; break; case ThirdParty.Weixin: authService = _weixinAuthService; break; case ThirdParty.Weibo: authService = _weiboAuthService; break; case ThirdParty.Alipay: authService = _alipayAuthService; break; default: throw new UserFriendlyException("不支持您所选的登录平台"); } //var codeCache = _cacheManager.GetCache("ThirdPartyAuthCodes"); //var codeStatus = codeCache.GetOrDefault(input.Code); //if (codeStatus != null) //{ // throw new UserFriendlyException("认证信息已失效,请您重试第三方登录认证"); //} //codeCache.Set(input.Code, input.Code, TimeSpan.FromMinutes(5)); var authorizeResult = authService.Authorize(new AuthorizationInput { Code = input.Code }); if (authorizeResult.Success) { var user = await _userManager.FindByIdAsync(authorizeResult.ThirdPartyUser.UserId); var identity = await _userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ExternalBearer); var ticket = new AuthenticationTicket(identity, new AuthenticationProperties()); var currentUtc = new SystemClock().UtcNow; ticket.Properties.IssuedUtc = currentUtc; ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromDays(3)); return(new AjaxResponse(AccountController.OAuthBearerOptions.AccessTokenFormat.Protect(ticket))); } return(new AjaxResponse(authorizeResult)); }
/// <inheritdoc /> public async Task <JsonResult> LoginUserBindingThirdParty(ThirdPartyLoginModel input) { try { IThirdPartyAuthService authService; switch (input.ThirdParty) { case ThirdParty.QQ: authService = _qqAuthService; break; case ThirdParty.Weixin: authService = _weixinAuthService; break; case ThirdParty.Weibo: authService = _weiboAuthService; break; case ThirdParty.Alipay: authService = _alipayAuthService; break; default: throw new UserFriendlyException("不支持您所选的登录平台"); } var authorizeResult = authService.Authorize(new AuthorizationInput { Code = input.Code }); if (!AuthenticationManager.User.Identity.IsAuthenticated) { throw new UserFriendlyException("当前用户没有登录"); } var userid = AuthenticationManager.User.Identity.GetUserId <long>(); var user = await _userManager.FindByIdAsync(userid); await _userRegistrationManager.BindingThirdPartyAsync(authorizeResult.Token, user); return(Json(new AjaxResponse(new { success = true, message = "", platform = input.ThirdParty.GetDescription() }))); } catch (Exception ex) { return(Json(new AjaxResponse(new { success = false, message = ex.Message, platform = input.ThirdParty.GetDescription() }))); } }
public async Task <JsonResult> ThirdPartyLogin(ThirdPartyLoginModel input) { try { IThirdPartyAuthService authService; switch (input.ThirdParty) { case ThirdParty.QQ: authService = _qqAuthService; break; case ThirdParty.Weixin: authService = _weixinAuthService; break; case ThirdParty.Weibo: authService = _weiboAuthService; break; case ThirdParty.Alipay: authService = _alipayAuthService; break; default: throw new UserFriendlyException("不支持您所选的登录平台"); } var codeCache = _cacheManager.GetCache("ThirdPartyAuthCodes"); var codeStatus = codeCache.GetOrDefault(input.Code); if (codeStatus != null) { throw new UserFriendlyException("认证信息已失效,请您重试第三方登录认证"); } codeCache.Set(input.Code, input.Code, TimeSpan.FromMinutes(5)); var authorizeResult = authService.Authorize(new AuthorizationInput { Code = input.Code }); if (authorizeResult.Success) { var user = await _userManager.FindByIdAsync(authorizeResult.ThirdPartyUser.UserId); //if (_settingManager.GetSettingValueForApplication<bool>(AppSettingNames.UserManagement.IsPhoneNumberConfirmationRequiredForLogin) // && !user.IsPhoneNumberConfirmed) //{ // throw new UserFriendlyException(L("LoginFailed"), "没有验证手机号"); //} //if (_settingManager.GetSettingValueForApplication<bool>(AbpZeroSettingNames.UserManagement.IsEmailConfirmationRequiredForLogin) // && !user.IsEmailConfirmed) //{ // throw new UserFriendlyException(L("LoginFailed"), "没有验证邮箱地址"); //} await SignInAsync(user); } return(Json(new AjaxResponse(authorizeResult))); } catch (Exception ex) { LogHelper.LogException(Logger, ex); Response.StatusCode = (int)HttpStatusCode.InternalServerError; EventBus.Trigger(this, new AbpHandledExceptionData(ex)); return(Json(new AjaxResponse(ErrorInfoBuilder.BuildForException(ex)))); } }