C# 强制用户登录MVC

C# 强制用户登录MVC,c#,asp.net-mvc,C#,Asp.net Mvc,我有一个MVC5站点(基于nopCommerce),我需要强制所有用户登录。我过去用其他语言做这件事的方式是,如果用户登录,检查页面标题(因此它在每个页面上),如果用户没有登录,则重定向到登录页面。然而,我不知道在MVC中实现这一点的正确方法。我找到的其他答案说重定向在MVC中不是一件可以做的事情,这很好,但我不知道如何正确地完成这一点。如何将所有未经授权的用户重定向到登录页面?您需要使用属性 关于重定向,来自MSDN: 如果未经授权的用户试图访问标记为的方法 在Authorize属性中,MVC

我有一个MVC5站点(基于nopCommerce),我需要强制所有用户登录。我过去用其他语言做这件事的方式是,如果用户登录,检查页面标题(因此它在每个页面上),如果用户没有登录,则重定向到登录页面。然而,我不知道在MVC中实现这一点的正确方法。我找到的其他答案说重定向在MVC中不是一件可以做的事情,这很好,但我不知道如何正确地完成这一点。如何将所有未经授权的用户重定向到登录页面?

您需要使用属性

关于重定向,来自MSDN:

如果未经授权的用户试图访问标记为的方法 在Authorize属性中,MVC框架返回401HTTP状态 代码。如果站点配置为使用ASP.NET窗体身份验证, 401状态代码使浏览器将用户重定向到 登录页面

因此,您可以在web.config中对登录视图应用重定向,或者如果您使用的是ASP.NET表单身份验证,则它应该根据您的需要重定向到登录页面

有四种实现Authorize属性的方法:全局、每个控制器、每个控制器方法或每个视图

全球:

[Authorize] 
public class AccountController : Controller
{
    . . .
}
public class AccountController : Controller
{
    . . .

    [AllowAnonymous]
    public ActionResult Register() { . . . }

    [Authorize]
    public ActionResult Manage() { . . . }

    . . .
}
<!-- With razor syntax -->

<script type="text/javascript>
    @if(!Request.IsAuthenticated) {
        window.location.href = redirectURL;
    }
</script>

<!-- Without razor syntax -->

<script type="text/javascript>  
    <% if(!Request.IsAuthenticated) { %>
        window.location.href = redirectURL;
    <% } %>
</script>
您可以在App_Start的“RegisterGlobalFilter”中添加“AuthorizeAttribute”,如下所示:

public class FilterConfig
{
    public static void RegisterGlobalFilter(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
        filters.Add(new AuthorizeAttribute());
    }
}
在Application_Start in Global.asax中,您应该注册此筛选器:

protected void Application_Start()
{
    FilterConfig.RegisterGlobalFilter(GlobalFilters.Filters);

    . . .
}
每个控制器:

[Authorize] 
public class AccountController : Controller
{
    . . .
}
public class AccountController : Controller
{
    . . .

    [AllowAnonymous]
    public ActionResult Register() { . . . }

    [Authorize]
    public ActionResult Manage() { . . . }

    . . .
}
<!-- With razor syntax -->

<script type="text/javascript>
    @if(!Request.IsAuthenticated) {
        window.location.href = redirectURL;
    }
</script>

<!-- Without razor syntax -->

<script type="text/javascript>  
    <% if(!Request.IsAuthenticated) { %>
        window.location.href = redirectURL;
    <% } %>
</script>
每个控制器方法:

[Authorize] 
public class AccountController : Controller
{
    . . .
}
public class AccountController : Controller
{
    . . .

    [AllowAnonymous]
    public ActionResult Register() { . . . }

    [Authorize]
    public ActionResult Manage() { . . . }

    . . .
}
<!-- With razor syntax -->

<script type="text/javascript>
    @if(!Request.IsAuthenticated) {
        window.location.href = redirectURL;
    }
</script>

<!-- Without razor syntax -->

<script type="text/javascript>  
    <% if(!Request.IsAuthenticated) { %>
        window.location.href = redirectURL;
    <% } %>
</script>
每个视图:

[Authorize] 
public class AccountController : Controller
{
    . . .
}
public class AccountController : Controller
{
    . . .

    [AllowAnonymous]
    public ActionResult Register() { . . . }

    [Authorize]
    public ActionResult Manage() { . . . }

    . . .
}
<!-- With razor syntax -->

<script type="text/javascript>
    @if(!Request.IsAuthenticated) {
        window.location.href = redirectURL;
    }
</script>

<!-- Without razor syntax -->

<script type="text/javascript>  
    <% if(!Request.IsAuthenticated) { %>
        window.location.href = redirectURL;
    <% } %>
</script>

您可以在App_Start的“RegisterGlobalFilter”中添加“AuthorizeAttribute”,如下所示:

public class FilterConfig
{
    public static void RegisterGlobalFilter(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
        filters.Add(new AuthorizeAttribute());
    }
}
在Application_Start in Global.asax中,您应该注册此筛选器:

    protected void Application_Start()
    {
        FilterConfig.RegisterGlobalFilter(GlobalFilters.Filters);

         .
         .
         .
    }
对于每个不需要用户注册的控制器,可以在控制器中的操作上使用“AllowAnonymous”属性,如下所示:

[AllowAnonymous]
public ActionResult Login()
{
    //do something
}

有关更多信息,请按照此操作,如果要授权所有控制器,您可以执行此操作…

在Global.asax中启动应用程序

GlobalFilters.Filters.Add(new AuthorizeAttribute());

看看这个。您需要确保对每个请求进行授权:如果我理解正确,这需要将
[authorize]
添加到每个视图的控制器功能中,这对于此站点来说太多了。请看以下内容:。