Exemple #1
0
        public async Task <AjaxResponse> Authenticate(LoginModel loginModel)
        {
            var loginResult = await _userManager.LoginAsync(loginModel.UserName, loginModel.Password);

            if (loginResult.Result == AbpLoginResultType.Success)
            {
                var ticket = new AuthenticationTicket(loginResult.Identity, new AuthenticationProperties());

                var currentUtc = new SystemClock().UtcNow;
                ticket.Properties.IssuedUtc  = currentUtc;
                ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromDays(30));
                return(new AjaxResponse(OAuthBearerOptions.AccessTokenFormat.Protect(ticket)));
            }
            else
            {
                switch (loginResult.Result)
                {
                case AbpLoginResultType.InvalidUserName:
                case AbpLoginResultType.InvalidPassword:
                    throw new UserFriendlyException(L("LoginFailed"), L("InvalidUserNameOrPassword"));

                case AbpLoginResultType.UserIsNotActive:
                    throw new UserFriendlyException(L("LoginFailed"), L("UserIsNotActiveAndCanNotLogin", loginModel.UserName));

                case AbpLoginResultType.EmailIsNotConfirmed:
                    throw new UserFriendlyException(L("LoginFailed"), "Your email address is not confirmed. You can not login"); //TODO: localize message

                default:                                                                                                         //Can not fall to default actually. But other result types can be added in the future and we may forget to handle it
                    Logger.Warn("Unhandled login fail reason: " + loginResult.Result);
                    throw new UserFriendlyException(L("LoginFailed"));
                }
            }
        }
        private async Task <AbpLoginResult> GetLoginResultAsync(string username, string password)
        {
            var loginResult = await _userManager.LoginAsync(username, password);

            switch (loginResult.Result)
            {
            case AbpLoginResultType.Success:
                return(loginResult);

            default:
                throw CreateExceptionForFailedLoginAttempt(loginResult.Result, username);
            }
        }