Skip to content

Instantly share code, notes, and snippets.

@msciborski
Created June 25, 2018 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msciborski/afc9eb219803d250b824ee668a5021bf to your computer and use it in GitHub Desktop.
Save msciborski/afc9eb219803d250b824ee668a5021bf to your computer and use it in GitHub Desktop.
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