blob: 9b531de49d4093f299024e9c736ddfd6d4923be2 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
namespace IOL.GreatOffice.Api.Services;
public class UserService
{
private readonly ForgotPasswordService _forgotPasswordService;
/// <summary>
/// Provides methods to perform common operations on user data.
/// </summary>
/// <param name="forgotPasswordService"></param>
public UserService(ForgotPasswordService forgotPasswordService) {
_forgotPasswordService = forgotPasswordService;
}
/// <summary>
/// Log in a user.
/// </summary>
/// <param name="httpContext"></param>
/// <param name="user"></param>
/// <param name="persist"></param>
public async Task LogInUser(HttpContext httpContext, User user, bool persist = false) {
var claims = new List<Claim> {
new(AppClaims.USER_ID, user.Id.ToString()),
new(AppClaims.NAME, user.Username),
};
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
var principal = new ClaimsPrincipal(identity);
var authenticationProperties = new AuthenticationProperties {
AllowRefresh = true,
IssuedUtc = DateTimeOffset.UtcNow,
};
if (persist) {
authenticationProperties.ExpiresUtc = DateTimeOffset.UtcNow.AddMonths(6);
authenticationProperties.IsPersistent = true;
}
await httpContext.SignInAsync(principal, authenticationProperties);
await _forgotPasswordService.DeleteRequestsForUserAsync(user.Id);
}
/// <summary>
/// Log out a user.
/// </summary>
/// <param name="httpContext"></param>
public async Task LogOutUser(HttpContext httpContext) {
await httpContext.SignOutAsync();
}
}
|