Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net vnext. mvc 6. Implement simple own authorization

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.

like image 473
Set Avatar asked Mar 26 '26 07:03

Set


1 Answers

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();
        }

    }
like image 150
htarikyavas Avatar answered Mar 28 '26 03:03

htarikyavas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!