Created
June 25, 2018 14:19
-
-
Save msciborski/afc9eb219803d250b824ee668a5021bf to your computer and use it in GitHub Desktop.
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
public class ValidateTokenAttribute : TypeFilterAttribute | |
{ | |
public ValidateTokenAttribute() : base(typeof(ValidateTokenFilterImpl)) | |
{ | |
} | |
public class ValidateTokenFilterImpl : IAsyncActionFilter | |
{ | |
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) | |
{ | |
if (context.ActionArguments.ContainsKey("userId")) | |
{ | |
var userId = context.ActionArguments["userId"] as string; | |
var userIdFromToken = context.HttpContext.User.Identity.Name; | |
if (!userId.Equals(userIdFromToken)) | |
{ | |
context.Result = new UnauthorizedResult(); | |
return; | |
} | |
} | |
await next(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment