Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Change AccessDenied route in ASP.NET Core

Suggested Videos
Part 94 - Claims based authorization in asp.net core | Text | Slides
Part 95 - Role based authorization vs claims based authorization in asp.net core | Text | Slides
Part 96 - Authorization in views in asp.net core mvc | Text | Slides

In this video we will discuss how to change the default AccessDenied route in ASP.NET Core

Default AccessDenied Route in ASP.NET Core

In ASP.NET Core if we try to access an unauthorized resource, by default we are redirected to /Account/AccessDenied path.


Change Default AccessDenied Route

To change the default access denied route, modify the code in ConfigureServices() method of the Startup class


public void ConfigureServices(IServiceCollection services)
{
    services.ConfigureApplicationCookie(options =>
    {
        options.AccessDeniedPath = new PathString("/Administration/AccessDenied");
    });
}

With the above change in place, if we try to access an unauthorized resource, we will be redirected to /Administration/AccessDenied path. Our obvious next step is to include AccessDenied action in the Administration controller and the corresponding view.

AccessDenied Action

[HttpGet]
[AllowAnonymous]
public IActionResult AccessDenied()
{
    return View();
}

AccessDenied View

<div class="text-center">
    <h1 class="text-danger">Access Denied</h1>
    <h6 class="text-danger">
        You do not have persmission to view this resource
    </h6>
    <img src="~/images/noaccess.png" style="height:300px; width:300px" />
</div>

asp.net core tutorial for beginners

1 comment:

  1. Thanks for your hard work and best tutorial.
    Can you please provide azure tutorial.
    Thanks,
    Sanjeev

    ReplyDelete

It would be great if you can help share these free resources