Skip to content

Commit 01f6feb

Browse files
Merge pull request dotnet-architecture#820 from erikpique/feature/118
Token lifetime handling dotnet-architecture#118
2 parents 4cb0e5c + 4f62175 commit 01f6feb

3 files changed

Lines changed: 28 additions & 8 deletions

File tree

src/Services/Identity/Identity.API/Controllers/AccountController.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,16 @@ public async Task<IActionResult> Login(LoginViewModel model)
7979
if (ModelState.IsValid)
8080
{
8181
var user = await _loginService.FindByUsername(model.Email);
82+
8283
if (await _loginService.ValidateCredentials(user, model.Password))
8384
{
84-
AuthenticationProperties props = null;
85+
var props = new AuthenticationProperties
86+
{
87+
ExpiresUtc = DateTimeOffset.UtcNow.AddHours(2),
88+
AllowRefresh = true,
89+
RedirectUri = model.ReturnUrl
90+
};
91+
8592
if (model.RememberMe)
8693
{
8794
props = new AuthenticationProperties
@@ -91,7 +98,7 @@ public async Task<IActionResult> Login(LoginViewModel model)
9198
};
9299
};
93100

94-
await _loginService.SignIn(user);
101+
await _loginService.SignInAsync(user, props);
95102

96103
// make sure the returnUrl is still valid, and if yes - redirect back to authorize endpoint
97104
if (_interaction.IsValidReturnUrl(model.ReturnUrl))

src/Services/Identity/Identity.API/Services/EFLoginService.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
using Microsoft.AspNetCore.Identity;
1+
using System.Threading.Tasks;
2+
using Microsoft.AspNetCore.Authentication;
3+
using Microsoft.AspNetCore.Identity;
24
using Microsoft.eShopOnContainers.Services.Identity.API.Models;
3-
using System.Threading.Tasks;
45

56
namespace Microsoft.eShopOnContainers.Services.Identity.API.Services
67
{
78
public class EFLoginService : ILoginService<ApplicationUser>
89
{
9-
UserManager<ApplicationUser> _userManager;
10-
SignInManager<ApplicationUser> _signInManager;
10+
private UserManager<ApplicationUser> _userManager;
11+
private SignInManager<ApplicationUser> _signInManager;
1112

12-
public EFLoginService(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager) {
13+
public EFLoginService(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager)
14+
{
1315
_userManager = userManager;
1416
_signInManager = signInManager;
1517
}
@@ -24,8 +26,14 @@ public async Task<bool> ValidateCredentials(ApplicationUser user, string passwor
2426
return await _userManager.CheckPasswordAsync(user, password);
2527
}
2628

27-
public Task SignIn(ApplicationUser user) {
29+
public Task SignIn(ApplicationUser user)
30+
{
2831
return _signInManager.SignInAsync(user, true);
2932
}
33+
34+
public Task SignInAsync(ApplicationUser user, AuthenticationProperties properties, string authenticationMethod = null)
35+
{
36+
return _signInManager.SignInAsync(user, properties, authenticationMethod);
37+
}
3038
}
3139
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
using System.Threading.Tasks;
2+
using Microsoft.AspNetCore.Authentication;
23

34
namespace Microsoft.eShopOnContainers.Services.Identity.API.Services
45
{
56
public interface ILoginService<T>
67
{
78
Task<bool> ValidateCredentials(T user, string password);
9+
810
Task<T> FindByUsername(string user);
11+
912
Task SignIn(T user);
13+
14+
Task SignInAsync(T user, AuthenticationProperties properties, string authenticationMethod = null);
1015
}
1116
}

0 commit comments

Comments
 (0)