How to apply custom validation to JWT token on each request for ASP.NET WebApi?

前端 未结 3 419
抹茶落季
抹茶落季 2021-02-04 07:43

Is it possible to add custom validation to each request when authenticating web api calls using a bearer token?

I\'m using the following configuration and the applicatio

3条回答
  •  你的背包
    2021-02-04 08:24

    The best way I would say is to write custom attribute. You need to inherit AuthorizeAttribute class and overridde AuthorizeCore method, there you can add custom validation.

    Once you are done, just decorate your controller or method with it.

    https://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute(v=vs.118).aspx

    Implementation example:

    public class MyCustomAttribute : AuthorizeAttribute
    {
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            // your validation here
        }
    }
    

    Usage examle:

    [MyCustom]
    public ActionResult MyAction()
    {
        return View();
    }
    

提交回复
热议问题