diff --git a/src/Volo.Abp.Account.Web.IdentityServer/Auth/SignInResultExtensions.cs b/src/Volo.Abp.Account.Web.IdentityServer/Auth/SignInResultExtensions.cs new file mode 100644 index 0000000000..ac229fbf52 --- /dev/null +++ b/src/Volo.Abp.Account.Web.IdentityServer/Auth/SignInResultExtensions.cs @@ -0,0 +1,33 @@ +using Microsoft.AspNetCore.Identity; + +namespace Volo.Abp.Account.Web.Auth +{ + //TODO: Move to the framework..? + public static class SignInResultExtensions + { + public static string GetResultAsString(this SignInResult signInResult) + { + if (signInResult.Succeeded) + { + return "Succeeded"; + } + + if (signInResult.IsLockedOut) + { + return "IsLockedOut"; + } + + if (signInResult.IsNotAllowed) + { + return "IsNotAllowed"; + } + + if (signInResult.RequiresTwoFactor) + { + return "RequiresTwoFactor"; + } + + return "Unknown"; + } + } +} diff --git a/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Login.cshtml.cs b/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Login.cshtml.cs index e280fcd7ce..ecc1abfbcf 100644 --- a/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Login.cshtml.cs +++ b/src/Volo.Abp.Account.Web.IdentityServer/Pages/Account/Login.cshtml.cs @@ -1,9 +1,11 @@ using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.Diagnostics; using System.Linq; using System.Security.Claims; using System.Threading.Tasks; +using IdentityServer4.Events; using IdentityServer4.Models; using IdentityServer4.Services; using IdentityServer4.Stores; @@ -12,6 +14,7 @@ using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using Volo.Abp.Account.Web.Auth; using Volo.Abp.Identity; using Volo.Abp.Ui; using Volo.Abp.Uow; @@ -46,6 +49,7 @@ namespace Volo.Abp.Account.Web.Pages.Account private readonly IAuthenticationSchemeProvider _schemeProvider; private readonly AbpAccountOptions _accountOptions; private readonly IClientStore _clientStore; + private readonly IEventService _identityServerEvents; public IdsLoginModel( SignInManager signInManager, @@ -53,13 +57,15 @@ namespace Volo.Abp.Account.Web.Pages.Account IIdentityServerInteractionService interaction, IAuthenticationSchemeProvider schemeProvider, IOptions accountOptions, - IClientStore clientStore) + IClientStore clientStore, + IEventService identityServerEvents) { _signInManager = signInManager; _userManager = userManager; _interaction = interaction; _schemeProvider = schemeProvider; _clientStore = clientStore; + _identityServerEvents = identityServerEvents; _accountOptions = accountOptions.Value; } @@ -120,7 +126,7 @@ namespace Volo.Abp.Account.Web.Pages.Account var context = await _interaction.GetAuthorizationContextAsync(ReturnUrl); if (context == null) { - return RedirectSafely("~/"); + return Redirect("~/"); } await _interaction.GrantConsentAsync(context, ConsentResponse.Denied); @@ -136,12 +142,23 @@ namespace Volo.Abp.Account.Web.Pages.Account true ); + //TODO: Find a way of getting user's id from the logged in user and do not query it again like that! + var user = await _userManager.FindByNameAsync(LoginInput.UserNameOrEmailAddress) ?? + await _userManager.FindByEmailAsync(LoginInput.UserNameOrEmailAddress); + if (!result.Succeeded) { + await _identityServerEvents.RaiseAsync(new UserLoginFailureEvent(LoginInput.UserNameOrEmailAddress, "Login failed: " + result.GetResultAsString())); throw new UserFriendlyException("Login failed!"); //TODO: Handle other cases, do not throw exception } - //var user = _userManager.FindByIdAsync(result.) + Debug.Assert(user != null, nameof(user) + " != null"); + await _identityServerEvents.RaiseAsync(new UserLoginSuccessEvent(user.UserName, user.Id.ToString(), user.UserName)); //TODO: Use user's name once implemented + + if (!_interaction.IsValidReturnUrl(ReturnUrl)) + { + return Redirect("~/"); + } return RedirectSafely(ReturnUrl, ReturnUrlHash); } diff --git a/src/Volo.Abp.Account.Web.IdentityServer/Volo.Abp.Account.Web.IdentityServer.csproj b/src/Volo.Abp.Account.Web.IdentityServer/Volo.Abp.Account.Web.IdentityServer.csproj index fa6ac31d6d..f5b527a464 100644 --- a/src/Volo.Abp.Account.Web.IdentityServer/Volo.Abp.Account.Web.IdentityServer.csproj +++ b/src/Volo.Abp.Account.Web.IdentityServer/Volo.Abp.Account.Web.IdentityServer.csproj @@ -19,14 +19,6 @@ - - - - - - - -