【问题标题】:Authentication for MVC3 areasMVC3 区域的身份验证
【发布时间】:2012-01-05 07:24:14
【问题描述】:

我正在使用 Areas 创建一个 mvc 应用程序。 我需要为这些区域设置不同的身份验证方法。

我通常使用 httpmodules 进行身份验证,在 http 模块中我检查用户是否经过身份验证(我通常使用 cookie),如果没有,我会重定向他。

所以我有一个用于验证整个应用程序的 httpmodule,我想注册另一个 httpmodule 以在该区域进行身份验证。

我试过了:

  1. 使用 area 文件夹中的 web.config 文件并在其中列出 httpmodule。
  2. 使用 web.config 文件中的位置部分。

两者都对我不起作用,httpmodule 从未被调用过。

  1. 如何为一个区域注册一个 httpmodule。
  2. 如何覆盖整个应用的 httpmodule。
  3. 如果这是错误的做法,那么更好的做法是什么。

谢谢

【问题讨论】:

    标签: asp.net-mvc-3 authentication httpmodule asp.net-mvc-3-areas


    【解决方案1】:

    您始终可以在覆盖 AuthorizeCore 的位置编写自定义 AuthorizeAttribute。在此功能中,您始终可以重定向到特定的登录网址。

    public class CustomAuthorizationAttribute : AuthorizeAttribute
    {
        private string notifyUrl = string.Format("{0}{1}", GeneralHelper.BaseSiteUrl, "Login");
    
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (httpContext.User.Identity.IsAuthenticated)
            {
                // get userinformation
                return true;
            }
    
            httpContext.Response.Redirect(NotifyUrl);
            return false;
        }
    }
    

    如果你不包含重定向,它将被重定向到 web.config 中定义的 loginurl

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-12
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多