Пример #1
0
        public virtual async Task <IActionResult> OnGetAsync()
        {
            if (CurrentUser.IsAuthenticated)
            {
                ToastHelper.ToastSuccess(this, $"{L["Welcome back!"]}, {CurrentUser.Name}");
                if (ReturnUrl.IsNullOrEmpty())
                {
                    return(Redirect("/"));
                }
                else
                {
                    return(RedirectSafely(ReturnUrl, ReturnUrlHash));
                }
            }

            ActionHelper.AddTitle(this, "Login");

            LoginInput = new LoginInputModel();

            ExternalProviders = await GetExternalProviders();

            EnableLocalLogin = await SettingProvider.IsTrueAsync(AccountSettingNames.EnableLocalLogin);

            if (IsExternalLoginOnly)
            {
                //return await ExternalLogin(vm.ExternalLoginScheme, returnUrl);
                throw new NotImplementedException();
            }

            return(Page());
        }
Пример #2
0
        private void SetUrls()
        {
            if (!_googleAuthUrl.IsNullOrEmpty())
            {
                return;
            }

            var client    = _auth0Helper.CreateAuthenticationApiClientIfNotExists();
            var returnUrl = $"{WebUtils.GetApplicationUrlPath(HttpContext.Current)}{CurrentUser.LanguageCode}/account/login-register/";

            if (!ReturnUrl.IsNullOrEmpty())
            {
                returnUrl = $"{returnUrl}?returnUrl={ReturnUrl}";
            }

            var google_authorizationUrl = client.BuildAuthorizationUrl()
                                          .WithResponseType(AuthorizationResponseType.Code)
                                          .WithClient(Auth0Helper.Auth0ClientId)
                                          .WithConnection("google-oauth2")
                                          .WithRedirectUrl(returnUrl)
                                          .WithScope("openid profile email")
                                          .Build();

            _googleAuthUrl = google_authorizationUrl.ToString();

            var facebook_authorizationUrl = client.BuildAuthorizationUrl()
                                            .WithResponseType(AuthorizationResponseType.Code)
                                            .WithClient(Auth0Helper.Auth0ClientId)
                                            .WithConnection("facebook")
                                            .WithRedirectUrl(returnUrl)
                                            .WithScope("openid profile email")
                                            .Build();

            _facebookAuthUrl = facebook_authorizationUrl.ToString();

            //var twitter_authorizationUrl = client.BuildAuthorizationUrl()
            //    .WithResponseType(AuthorizationResponseType.Code)
            //    .WithClient(Auth0Helper.Auth0ClientId)
            //    .WithConnection("twitter")
            //    .WithRedirectUrl(returnUrl)
            //    .WithScope("openid profile email")
            //    .Build();
            //_twitterAuthUrl = twitter_authorizationUrl.ToString();

            var linkedIn_authorizationUrl = client.BuildAuthorizationUrl()
                                            .WithResponseType(AuthorizationResponseType.Code)
                                            .WithClient(Auth0Helper.Auth0ClientId)
                                            .WithConnection("linkedin")
                                            .WithRedirectUrl(returnUrl)
                                            .WithScope("openid profile email")
                                            .Build();

            _linkedInAuthUrl = linkedIn_authorizationUrl.ToString();
        }
Пример #3
0
        public async Task <IActionResult> OnPostAsync(string button)
        {
            var context = await m_IdsInteraction.GetAuthorizationContextAsync(ReturnUrl);

            if (button != "login")
            {
                if (context != null)
                {
                    /*
                     * 如果用户点击取消,则向IdentityServer发送一个结果(类似于用户拒绝同意)】
                     * 然后Oidc错误会被发回给客户端
                     */
                    await m_IdsInteraction.DenyAuthorizationAsync(context, IdentityServer4.Models.AuthorizationError.AccessDenied);

                    if (context.IsNativeClient())
                    {
                        //The client is native, so this change in how to return the response is for better UX for the end user.
                        //客户端是本地客户端,因此此更改返回响应的方式是为最终用户提供更好的UX。
                        //TODO : 这边跳转往后有时间再写
                    }

                    return(Redirect("/Index"));
                }
            }

            if (ModelState.IsValid)
            {
                var result = await m_SignInManager.PasswordSignInAsync(LoginDto.Username, LoginDto.Password, LoginDto.RememberLogin, lockoutOnFailure : true);

                if (result.Succeeded)
                {
                    var user = await m_UserManager.FindByNameAsync(LoginDto.Username);

                    await m_IdsEvent.RaiseAsync(new UserLoginSuccessEvent(user.UserName, user.Id, user.UserName, clientId : context?.Client.ClientId));

                    if (context != null)
                    {
                        if (context.IsNativeClient())
                        {
                            //The client is native, so this change in how to return the response is for better UX for the end user.
                            //客户端是本地客户端,因此此更改返回响应的方式是为最终用户提供更好的UX。
                            //TODO : 这边跳转往后有时间再写
                        }

                        return(Redirect(ReturnUrl));
                    }

                    //请求来自本地页面
                    if (Url.IsLocalUrl(ReturnUrl))
                    {
                        return(Redirect(ReturnUrl));
                    }
                    else if (ReturnUrl.IsNullOrEmpty())
                    {
                        return(Redirect("/"));
                    }
                    else
                    {
                        // user might have clicked on a malicious link - should be logged
                        throw new Exception(L["invalid_return_URL", ReturnUrl]);
                    }
                }

                await m_IdsEvent.RaiseAsync(new UserLoginFailureEvent(LoginDto.Username, "invalid credentials", clientId : context?.Client.ClientId));

                ModelState.AddModelError(string.Empty, L["invalid_credentials_error_message"]);
            }


            return(Page());
        }