Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Redirect in AuthorizationHandler

$
0
0

Below is code for a simple AuthorizationHandler. In case of failure I want to redirect to a specific Controller/Action. How do I achieve this in ASP.NET 5?

With MVC5 we could do redirects in authorization filters however using policy and authorization handlers seem to be the recommended approach for ASP.NET 5. 

    public class AdminOnlyRequirement :
        AuthorizationHandler<AdminOnlyRequirement>, IAuthorizationRequirement
    {
        protected override void Handle(AuthorizationContext context,
                                       AdminOnlyRequirement requirement)
        {
            if (context.User.IsAdmin()) //IsAdmin() is custom extension Method
            {
                context.Succeed(requirement);
            }
            else
            {
                context.Fail();
            }
        }
    }


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>