-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
364c1de
commit 3694ba1
Showing
2 changed files
with
78 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
namespace Linker.WebApi.Filters; | ||
|
||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using System.Security.Claims; | ||
|
||
public sealed class AccountAuthorizeAttribute : Attribute, IAuthorizationFilter | ||
{ | ||
/// <inheritdoc/> | ||
public void OnAuthorization(AuthorizationFilterContext context) | ||
{ | ||
var userId = context.HttpContext.User.Claims | ||
.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier)?.Value; | ||
|
||
var routeParameterDictionary = context.HttpContext.GetRouteData(); | ||
var routeUserId = routeParameterDictionary.Values["userId"] | ||
?? throw new InvalidOperationException("The route has no accountId."); | ||
|
||
if (userId is null) | ||
{ | ||
goto ending; | ||
} | ||
|
||
if (routeUserId is string targetUserId && userId.Equals(targetUserId, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return; | ||
} | ||
|
||
ending: | ||
context.Result = new UnauthorizedResult(); | ||
} | ||
} |