【问题标题】:Problem restricting anonymous access to an ASP.Net MVC Site限制匿名访问 ASP.Net MVC 站点的问题
【发布时间】:2009-02-14 06:20:53
【问题描述】:

每当我在我的 MVC 站点中限制匿名访问时,我都会收到 404 错误:

“/”应用程序中的服务器错误。 无法找到该资源。 说明:HTTP 404。您要查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请查看以下 URL 并确保 > 拼写正确。

请求的 URL:/Account/Login

我刚刚第一次使用 MVC (RC1 Refresh),在让我退出的会员提供商正常工作后,我想锁定该网站以防止匿名访问。我尝试了使用 web.config 的传统方式:

<configuration>
    <system.web> 
        <authorization> 
            <deny users="?"/> 
        </authorization> 
    </system.web> 
</configuration>

但即使我明确允许匿名访问登录页面,也会出现上述错误。

我还尝试了Scott Gu's blog 中提到的技术,并通过在 HomeController 中添加 [Authorize] 属性来保护 About 页面

[Authorize]
public ActionResult About()
{
    return View();
}

但是当我尝试访问该页面时遇到了同样的错误。

我什至尝试在单独的机器上进行全新安装。

那么如何在 ASP.Net MVC RC1 Refresh 中启用授权?

【问题讨论】:

    标签: asp.net-mvc authorization


    【解决方案1】:

    默认 Web.Config 包含错误。它有:

    <authentication mode="Forms">
        <forms loginUrl="~/Account/Login"/>
    </authentication>
    

    这应该是:

    <authentication mode="Forms">
        <forms loginUrl="~/Account/LogOn"/>
    </authentication>
    

    (对不起,我问并回答了我自己的问题,但我花了很长时间才发现这一点,并且无法通过 Google 或 SO 找到任何线索。如果之前已经发布过,请随时关闭)。

    【讨论】:

    • 谢谢,这可能是几个小时的鹅追逐。
    【解决方案2】:

    我不建议使用表单身份验证。

    改为使用中间件管道。

    public void ConfigureAuth(IAppBuilder app)
    {
        // Enable the application to use a cookie to store information for the signed in user
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login")
        });
    }
    

    当然,您需要从 web 配置中删除 formsauthentication 模块并使用 [Authorize] 关键字

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-15
      • 1970-01-01
      • 2010-11-25
      • 1970-01-01
      • 2012-03-25
      相关资源
      最近更新 更多