/// <inheritdoc /> public async Task <JsonResult> BindingThirdParty(BindingThirdPartyInput input) { try { var result = await _logInManager.LoginAsync(input.UserName, input.Password); if (result.Result != AbpLoginResultType.Success) { throw new UserFriendlyException("用户名或密码输入错误"); } if (string.IsNullOrEmpty(input.Token)) { throw new UserFriendlyException("第三方认证令牌有误或者已失效,请重新绑定"); } await _userRegistrationManager.BindingThirdPartyAsync(input.Token, result.User); await SignInAsync(result.User); return(Json(new AjaxResponse())); } 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)))); } }
public JsonResult ScanQRCode(QRLoginInput input) { if (!AbpSession.UserId.HasValue) { var errorInfo = ErrorInfoBuilder.BuildForException(new Abp.UI.UserFriendlyException("请先在手机上登录")); errorInfo.Code = 401; return(Json(new AjaxResponse(errorInfo))); } var findCode = _cacheManager.GetCache("QRLoginHub").GetOrDefault <string, QRCodeInfo>(input.ConnectionId); if (findCode == null) { return(Json(new AjaxResponse(ErrorInfoBuilder.BuildForException(new Abp.UI.UserFriendlyException("没有找到会话"))))); } if (findCode.Token != input.Token) { return(Json(new AjaxResponse(ErrorInfoBuilder.BuildForException(new Abp.UI.UserFriendlyException("参数验证错误"))))); } if (!findCode.IsValid()) { return(Json(new AjaxResponse(ErrorInfoBuilder.BuildForException(new Abp.UI.UserFriendlyException("二维码已过期"))))); } QRLoginHub.Clients.Client(input.ConnectionId).scanQRCode(); return(Json(new AjaxResponse(true))); }
protected virtual ActionResult GenerateJsonExceptionResult(ExceptionContext context) { context.HttpContext.Items.Add("IgnoreJsonRequestBehaviorDenyGet", "true"); return(new AbpJsonResult( new AjaxResponse( ErrorInfoBuilder.BuildForException(context.Exception), context.Exception is AbpAuthorizationException ) )); }
public JsonResult ThirdPartyList() { try { var result = new List <ThirdPartyModel>(); if (SettingManager.GetSettingValue <bool>(AppSettingNames.OAuth.QQ.IsEnabled)) { result.Add(new ThirdPartyModel { ThirdParty = ThirdParty.QQ.ToString(), ThirdPartyName = "QQ", AuthUrl = _qqAuthService.GetAuthRedirectUrl(), IconUrl = "/Images/qq.png" }); } if (SettingManager.GetSettingValue <bool>(AppSettingNames.OAuth.WeixinOpen.IsEnabled)) { result.Add(new ThirdPartyModel { ThirdParty = ThirdParty.Weixin.ToString(), ThirdPartyName = "微信", AuthUrl = _weixinAuthService.GetAuthRedirectUrl(), IconUrl = "/Images/wechat.png" }); } if (SettingManager.GetSettingValue <bool>(AppSettingNames.OAuth.Weibo.IsEnabled)) { result.Add(new ThirdPartyModel { ThirdParty = ThirdParty.Weibo.ToString(), ThirdPartyName = "微博", AuthUrl = _weiboAuthService.GetAuthRedirectUrl(), IconUrl = "/Images/weibo.png" }); } if (SettingManager.GetSettingValue <bool>(AppSettingNames.OAuth.Alipay.IsEnabled)) { result.Add(new ThirdPartyModel { ThirdParty = ThirdParty.Alipay.ToString(), ThirdPartyName = "支付宝", AuthUrl = _alipayAuthService.GetAuthRedirectUrl(), IconUrl = "/Images/alipay.png" }); } return(Json(new AjaxResponse(result))); } 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)))); } }
public async Task <JsonResult> Login(string token) { long userId = _cacheManager.GetCache("QRLoginToken").GetOrDefault <string, long>(token); if (userId == default(long)) { return(Json(new AjaxResponse(ErrorInfoBuilder.BuildForException(new Abp.UI.UserFriendlyException("验证失败"))))); } var user = await _userManager.GetUserByIdAsync(userId); var identity = await _userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie); AuthenticationManager.SignIn(new AuthenticationProperties(), identity); _cacheManager.GetCache("QRLoginToken").Remove(token); return(Json(new AjaxResponse(true))); }
public async Task <JsonResult> Login(LoginViewModel loginModel, string returnUrl = "", string returnUrlHash = "") { try { CheckModelState(); _captchaManager.CheckCaptcha(loginModel.Captcha); await _logInManager.CheckLoginSetting(loginModel.UsernameOrEmailAddress); var loginResult = await GetLoginResultAsync( loginModel.UsernameOrEmailAddress, loginModel.Password, "Default" //GetTenancyNameOrNull() ); await SignInAsync(loginResult.User, loginResult.Identity, loginModel.RememberMe); if (string.IsNullOrWhiteSpace(returnUrl)) { returnUrl = Request.ApplicationPath; } if (!string.IsNullOrWhiteSpace(returnUrlHash)) { returnUrl = returnUrl + returnUrlHash; } return(Json(new AjaxResponse(new { ShouldChangePasswordOnNextLogin = loginResult.User.ShouldChangePasswordOnNextLogin }) { TargetUrl = returnUrl })); } 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)))); } }
/// <inheritdoc /> public async Task <JsonResult> LoginUserUnbindingThirdParty(UnbindingThirdPartyModel input) { try { if (!AuthenticationManager.User.Identity.IsAuthenticated) { throw new UserFriendlyException("当前用户没有登录"); } var userid = AuthenticationManager.User.Identity.GetUserId <long>(); var user = await _userManager.FindByIdAsync(userid); await _userRegistrationManager.UnbindingThirdPartyAsync(input.ThirdParty.ToString(), user); return(Json(new AjaxResponse())); } 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)))); } }
public JsonResult ConfirmLogin(QRLoginInput input) { var findCode = _cacheManager.GetCache("QRLoginHub").GetOrDefault <string, QRCodeInfo>(input.ConnectionId); if (findCode == null) { return(Json(new AjaxResponse(ErrorInfoBuilder.BuildForException(new Abp.UI.UserFriendlyException("没有找到会话"))))); } if (findCode.Token != input.Token) { return(Json(new AjaxResponse(ErrorInfoBuilder.BuildForException(new Abp.UI.UserFriendlyException("参数验证错误"))))); } if (!findCode.IsValid()) { return(Json(new AjaxResponse(ErrorInfoBuilder.BuildForException(new Abp.UI.UserFriendlyException("二维码已过期"))))); } string token = Guid.NewGuid().ToString() + Guid.NewGuid().ToString(); _cacheManager.GetCache("QRLoginHub").Remove(input.ConnectionId); _cacheManager.GetCache("QRLoginToken").Set(token, AbpSession.UserId.Value); QRLoginHub.Clients.Client(input.ConnectionId).confirmLogin(token); return(Json(new AjaxResponse(true))); }
protected virtual ActionResult GenerateNonJsonExceptionResult(ExceptionContext context) { return(new ViewResult { ViewName = "Error", MasterName = string.Empty, ViewData = new ViewDataDictionary <ErrorViewModel>(new ErrorViewModel(ErrorInfoBuilder.BuildForException(context.Exception), context.Exception)), TempData = context.Controller.TempData }); }
public async Task <JsonResult> GetBindingThirdPartyList() { try { if (!AuthenticationManager.User.Identity.IsAuthenticated) { throw new UserFriendlyException("当前用户没有登录"); } var userid = AuthenticationManager.User.Identity.GetUserId <long>(); var bindingUsers = await _userRegistrationManager.GetBindingUsersAsync(userid); var result = new List <ThirdPartyModel>(); if (SettingManager.GetSettingValue <bool>(AppSettingNames.OAuth.QQ.IsEnabled)) { result.Add(new ThirdPartyModel { ThirdParty = ThirdParty.QQ.ToString(), ThirdPartyName = "QQ", AuthUrl = _qqAuthService.GetBindingRedirectUrl(), IconUrl = "/Images/qq.png", IsBinding = bindingUsers.Exists(u => u.ThirdParty == ThirdParty.QQ.ToString()) }); } if (SettingManager.GetSettingValue <bool>(AppSettingNames.OAuth.WeixinOpen.IsEnabled)) { result.Add(new ThirdPartyModel { ThirdParty = ThirdParty.Weixin.ToString(), ThirdPartyName = "微信", AuthUrl = _weixinAuthService.GetBindingRedirectUrl(), IconUrl = "/Images/wechat.png", IsBinding = bindingUsers.Exists(u => u.ThirdParty == ThirdParty.Weixin.ToString()) }); } if (SettingManager.GetSettingValue <bool>(AppSettingNames.OAuth.Weibo.IsEnabled)) { result.Add(new ThirdPartyModel { ThirdParty = ThirdParty.Weibo.ToString(), ThirdPartyName = "微博", AuthUrl = _weiboAuthService.GetBindingRedirectUrl(), IconUrl = "/Images/weibo.png", IsBinding = bindingUsers.Exists(u => u.ThirdParty == ThirdParty.Weibo.ToString()) }); } if (SettingManager.GetSettingValue <bool>(AppSettingNames.OAuth.Alipay.IsEnabled)) { result.Add(new ThirdPartyModel { ThirdParty = ThirdParty.Alipay.ToString(), ThirdPartyName = "支付宝", AuthUrl = _alipayAuthService.GetBindingRedirectUrl(), IconUrl = "/Images/alipay.png", IsBinding = bindingUsers.Exists(u => u.ThirdParty == ThirdParty.Alipay.ToString()) }); } return(Json(new AjaxResponse(result))); } 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)))); } }
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)))); } }