Code: Select all
[HttpPost("logout")]
[Authorize]
public async Task Logout()
{
var logoutResult = await _authService.Logout();
await HttpContext.SignOutAsync(IdentityConstants.ApplicationScheme);
await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
await HttpContext.SignOutAsync(IdentityConstants.TwoFactorRememberMeScheme);
await HttpContext.SignOutAsync(IdentityConstants.TwoFactorUserIdScheme);
// added these, but still no success
if (!logoutResult.IsSuccess)
{
return BadRequest(logoutResult);
}
return logoutResult.Value;
}
[HttpGet("me")]
[Authorize]
public async Task GetCurrentUser()
{
var userId = User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier)?.Value;
// User is logged in, even after calling the log out endpoint
if (string.IsNullOrWhiteSpace(userId))
{
return Unauthorized();
}
var userResult = await _authService.GetLoggedUser(userId!);
if (userResult.Value.Id == Guid.Empty)
{
return Unauthorized();
}
return userResult;
}
// AuthService is a scoped service where *UserManager* and *SignInManager* are injected:
public async Task Login(LoginModel loginModel)
{
var user = await _context.Users
.Include(u => u.Tenant)
.FirstOrDefaultAsync(u => u.Email == loginModel.Email);
// validations ommitted
var token = await _tokenService.GenerateJwtToken(user.Id);
var roles = await _userManager.GetRolesAsync(user);
var permissions = await _permissionService.GetRolePermissions(roles);
var expiryInMinutes = int.Parse(_configuration.GetSection("JwtSettings") ["ExpiryInMinutes"]!);
return new AuthResponse
{
Token = token,
Expiration = DateTime.UtcNow.AddMinutes(expiryInMinutes),
Email = user.Email,
Roles = roles.ToList(),
Permissions = permissions,
Tenant = new TenantInfo(user.Tenant.Id, user.Tenant.Name)
};
}
public async Task Logout()
{
await _signInManager.SignOutAsync();
return new AuthResponse();
}`
// swagger options to enable authorization
options.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Name = "Authorization",
Type = SecuritySchemeType.Http,
Scheme = "Bearer",
BearerFormat = "JWT",
In = ParameterLocation.Header,
Description = "Enter your JWT token"
});
options.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
Array.Empty()
}
});`
Ich überprüfe nur, ob das korrekt ist und die App den Benutzer immer noch ordnungsgemäß abmeldet, wenn sie vom Frontent aufgerufen wird?
Mobile version