How to redirect unauthorized users using ASP.NET MVC 6

I want to know how to redirect users. I have a controller index (), and I want only users with the Student role to be able to enter it! Therefore i use

[Authorize(Roles="Student")]

I wonder how I can redirect users who do not have this role on the main page.

+4
source share
2 answers

MVC5 (and older):

You can do this by changing the attribute loginUrlto web.config . Change it to the desired route:

<authentication mode="Forms">
  <forms loginUrl="~/Home/Index" timeout="2880" />
</authentication>

MVC6:

In MVC6, you can try this (internally Startup.cs):

public void ConfigureServices(IServiceCollection services)
{       
    services.Configure<CookieAuthenticationOptions>(options =>
    {
        options.LoginPath = new PathString("/Home/Index");
    });
}
+7
source

?

, , ASAP

.

<%If Session("userRole")="Student" Then%>
  This is the text version of the page
<%Else%>
  Response.Redirect("notavailablepage.html")
<%End If%>
-6

Source: https://habr.com/ru/post/1622805/


All Articles