Skip to content

Commit

Permalink
Implement proposal Improve authorization performance jasontaylordev#1046
Browse files Browse the repository at this point in the history


Moved Roles authorization from Identity Service DB verification to verify Roles user Claim.
  • Loading branch information
Lanz86 committed Apr 20, 2024
1 parent 357a072 commit d17c806
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public async Task<TResponse> Handle(TRequest request, RequestHandlerDelegate<TRe
{
foreach (var role in roles)
{
var isInRole = await _identityService.IsInRoleAsync(_user.Id, role.Trim());
var isInRole = _user.Roles?.Any(x => role == x)??false;
if (isInRole)
{
authorized = true;
Expand Down
2 changes: 2 additions & 0 deletions src/Application/Common/Interfaces/IUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@
public interface IUser
{
string? Id { get; }
List<string>? Roles { get; }

}
2 changes: 2 additions & 0 deletions src/Web/Services/CurrentUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public CurrentUser(IHttpContextAccessor httpContextAccessor)
}

public string? Id => _httpContextAccessor.HttpContext?.User?.FindFirstValue(ClaimTypes.NameIdentifier);
public List<string>? Roles => _httpContextAccessor.HttpContext?.User?.FindAll(ClaimTypes.Role).Select(x => x.Value).ToList();

}

0 comments on commit d17c806

Please sign in to comment.