如何未经授权的用户重定向的ASP.NET MVC 6 [英] How to redirect unauthorized users with ASP.NET MVC 6

查看:157
本文介绍了如何未经授权的用户重定向的ASP.NET MVC 6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将用户重定向。我有一个控制器指数(),我只希望用户角色学生能进入那里!所以我用

I want to know how to redirect users. I have a Controller Index() and I want only users with the role "Student" can enter there! So I use

[Authorize(Roles="Student")]

我不知道如何可以重定向没有这个角色的网页谁的用户

I wonder how can I redirect users who do not have this role to the homepage

推荐答案

您可以通过您的的web.config 修改 loginUrl 属性做到这一点。更改为你想要的路线。

MVC5 (and older):

You can do this by changing the loginUrl attribute on your web.config. Change to the route you want.

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

MVC6

在MVC6你可以试试这个(在 Startup.cs

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

这篇关于如何未经授权的用户重定向的ASP.NET MVC 6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆