I am writing simple REST service for leaning vNext and new MVC. I want to understand how to create a simple OWIN authorization with custom logic. For example, imagine that a service has just a simple in-memory list of tokens and I want to check that a request contains token that exist in that list.
If I understood properly, I just need to override AuthorizeAttribute class, but I cannot find how to do this in the right way.
public class CustomAuthorize : AuthorizeAttribute
{
public override Task OnAuthorizationAsync(AuthorizationContext context)
{
return base.OnAuthorizationAsync(context);
}
}
If I misunderstood this, could you please explain what classes I need to use and can I do it.
You can use :
public class AuthorizeClass: ActionFilterAttribute
{
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
if (context.HttpContext.Session["token"] == null || context.HttpContext.Session["user"] == null)
{
context.HttpContext.Response.Redirect("/login");
}
await next();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With