Erik Pique 6 years ago
parent
commit
74924d975d
3 changed files with 38 additions and 18 deletions
  1. +19
    -12
      src/Services/Identity/Identity.API/Controllers/AccountController.cs
  2. +14
    -6
      src/Services/Identity/Identity.API/Services/EFLoginService.cs
  3. +5
    -0
      src/Services/Identity/Identity.API/Services/ILoginService.cs

+ 19
- 12
src/Services/Identity/Identity.API/Controllers/AccountController.cs View File

@ -1,4 +1,9 @@
using IdentityModel;
using System;
using System.Linq;
using System.Security.Claims;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using IdentityModel;
using IdentityServer4; using IdentityServer4;
using IdentityServer4.Models; using IdentityServer4.Models;
using IdentityServer4.Services; using IdentityServer4.Services;
@ -11,11 +16,6 @@ using Microsoft.eShopOnContainers.Services.Identity.API.Models;
using Microsoft.eShopOnContainers.Services.Identity.API.Models.AccountViewModels; using Microsoft.eShopOnContainers.Services.Identity.API.Models.AccountViewModels;
using Microsoft.eShopOnContainers.Services.Identity.API.Services; using Microsoft.eShopOnContainers.Services.Identity.API.Services;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using System.Security.Claims;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
namespace Microsoft.eShopOnContainers.Services.Identity.API.Controllers namespace Microsoft.eShopOnContainers.Services.Identity.API.Controllers
{ {
@ -79,9 +79,16 @@ namespace Microsoft.eShopOnContainers.Services.Identity.API.Controllers
if (ModelState.IsValid) if (ModelState.IsValid)
{ {
var user = await _loginService.FindByUsername(model.Email); var user = await _loginService.FindByUsername(model.Email);
if (await _loginService.ValidateCredentials(user, model.Password)) if (await _loginService.ValidateCredentials(user, model.Password))
{ {
AuthenticationProperties props = null;
var props = new AuthenticationProperties
{
ExpiresUtc = DateTimeOffset.UtcNow.AddHours(2),
AllowRefresh = true,
RedirectUri = model.ReturnUrl
};
if (model.RememberMe) if (model.RememberMe)
{ {
props = new AuthenticationProperties props = new AuthenticationProperties
@ -91,8 +98,8 @@ namespace Microsoft.eShopOnContainers.Services.Identity.API.Controllers
}; };
}; };
await _loginService.SignIn(user);
await _loginService.SignInAsync(user, props);
// make sure the returnUrl is still valid, and if yes - redirect back to authorize endpoint // make sure the returnUrl is still valid, and if yes - redirect back to authorize endpoint
if (_interaction.IsValidReturnUrl(model.ReturnUrl)) if (_interaction.IsValidReturnUrl(model.ReturnUrl))
{ {
@ -113,7 +120,7 @@ namespace Microsoft.eShopOnContainers.Services.Identity.API.Controllers
return View(vm); return View(vm);
} }
async Task<LoginViewModel> BuildLoginViewModelAsync(string returnUrl, AuthorizationRequest context)
private async Task<LoginViewModel> BuildLoginViewModelAsync(string returnUrl, AuthorizationRequest context)
{ {
var allowLocal = true; var allowLocal = true;
if (context?.ClientId != null) if (context?.ClientId != null)
@ -132,7 +139,7 @@ namespace Microsoft.eShopOnContainers.Services.Identity.API.Controllers
}; };
} }
async Task<LoginViewModel> BuildLoginViewModelAsync(LoginViewModel model)
private async Task<LoginViewModel> BuildLoginViewModelAsync(LoginViewModel model)
{ {
var context = await _interaction.GetAuthorizationContextAsync(model.ReturnUrl); var context = await _interaction.GetAuthorizationContextAsync(model.ReturnUrl);
var vm = await BuildLoginViewModelAsync(model.ReturnUrl, context); var vm = await BuildLoginViewModelAsync(model.ReturnUrl, context);
@ -193,7 +200,7 @@ namespace Microsoft.eShopOnContainers.Services.Identity.API.Controllers
try try
{ {
// hack: try/catch to handle social providers that throw // hack: try/catch to handle social providers that throw
await HttpContext.SignOutAsync(idp, new AuthenticationProperties await HttpContext.SignOutAsync(idp, new AuthenticationProperties
{ {


+ 14
- 6
src/Services/Identity/Identity.API/Services/EFLoginService.cs View File

@ -1,15 +1,17 @@
using Microsoft.AspNetCore.Identity;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Identity;
using Microsoft.eShopOnContainers.Services.Identity.API.Models; using Microsoft.eShopOnContainers.Services.Identity.API.Models;
using System.Threading.Tasks;
namespace Microsoft.eShopOnContainers.Services.Identity.API.Services namespace Microsoft.eShopOnContainers.Services.Identity.API.Services
{ {
public class EFLoginService : ILoginService<ApplicationUser> public class EFLoginService : ILoginService<ApplicationUser>
{ {
UserManager<ApplicationUser> _userManager;
SignInManager<ApplicationUser> _signInManager;
private UserManager<ApplicationUser> _userManager;
private SignInManager<ApplicationUser> _signInManager;
public EFLoginService(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager) {
public EFLoginService(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager)
{
_userManager = userManager; _userManager = userManager;
_signInManager = signInManager; _signInManager = signInManager;
} }
@ -24,8 +26,14 @@ namespace Microsoft.eShopOnContainers.Services.Identity.API.Services
return await _userManager.CheckPasswordAsync(user, password); return await _userManager.CheckPasswordAsync(user, password);
} }
public Task SignIn(ApplicationUser user) {
public Task SignIn(ApplicationUser user)
{
return _signInManager.SignInAsync(user, true); return _signInManager.SignInAsync(user, true);
} }
public Task SignInAsync(ApplicationUser user, AuthenticationProperties properties, string authenticationMethod = null)
{
return _signInManager.SignInAsync(user, properties, authenticationMethod);
}
} }
} }

+ 5
- 0
src/Services/Identity/Identity.API/Services/ILoginService.cs View File

@ -1,11 +1,16 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
namespace Microsoft.eShopOnContainers.Services.Identity.API.Services namespace Microsoft.eShopOnContainers.Services.Identity.API.Services
{ {
public interface ILoginService<T> public interface ILoginService<T>
{ {
Task<bool> ValidateCredentials(T user, string password); Task<bool> ValidateCredentials(T user, string password);
Task<T> FindByUsername(string user); Task<T> FindByUsername(string user);
Task SignIn(T user); Task SignIn(T user);
Task SignInAsync(T user, AuthenticationProperties properties, string authenticationMethod = null);
} }
} }

Loading…
Cancel
Save