0

我在 VS2013(和 IIS)的默认文档上收到 401.2 错误。以下是我正在采取的步骤:

  1. 在VS2013中,右键选择“新建项目”
  2. 选择“ASP.NET Web 应用程序”,点击确定
  3. 选择“Empty”项目,勾选底部的“Web Forms”并点击OK
  4. 右键单击项目并选择“添加 | Web 表单” - 命名为 Default.aspx 并以“身份验证成功”作为页面内容
  5. 右键单击项目并选择“添加 | Web 表单” - 命名为 Login.aspx
  6. 添加“登录”作为页面内容
  7. 分配设置“e.Authenticated = true”的“身份验证”事件处理程序
  8. 如下所示更新 web.config
  9. 按 F5

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <authentication mode="Forms">
      <forms loginUrl="Login.aspx" />
    </authentication>
  </system.web>
  <system.webServer>
    <defaultDocument>
      <files>
        <clear />
        <add value="default.aspx" />
      </files>
    </defaultDocument>
    <security>
      <authorization>
        <remove users="*"/>
        <add accessType="Deny" users="?" />
        <add accessType="Allow" users="*"/>
      </authorization>
    </security>
  </system.webServer>
</configuration>

我看到的行为是http://localhost:12345/Default.aspx行为正确(总是)。换句话说,当我第一次访问 Default.aspx 时,它会将我重定向到 Login.aspx 页面。完成身份验证后,我可以看到 Default.aspx 页面。如果我注销并再次尝试转到 Default.aspx 页面,它会将我重定向到先登录。

但是,当我得到 / URL(没有 Default.aspx)时,我得到一个 401.2 错误(即使我已经通过了第一个身份验证)?

Default.aspx 页面被列为默认文档,如果我从 Web.Config 中删除“拒绝”行,则默认文档的行为与预期相同。但是什么时候拒绝?在 web 配置中列出,突然默认文档停止工作,我必须转到 /Default.aspx 以避免 401.2 错误。

任何建议为什么会这样?

我在事件日志中没有看到任何错误。使用 IISExpress(在 VS 中按 F5)或使用 IIS 直接通过浏览器访问公共 URL 时,我看到了相同的行为。

4

1 回答 1

2

我犹豫是否提供这个作为答案,因为我不明白为什么它对我有用。但是,评论太长了,它可能会对您有所帮助。

我发现将以下内容添加到该System.Webserver部分可以解决此问题:

<modules>
<remove name="FormsAuthentication"/>
<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule"  />
</modules> 

关键似乎是managedHandler从 FormsAuthentication 模块中删除前提条件。据我了解,这只是为了优化静态内容的服务。所以我现在不知道为什么会产生这种效果。我偶然发现了这个试图确定 FormsAuthentication 模块是否需要在该System.Webserver部分中注册。

于 2015-02-24T16:14:26.837 回答