HTTP错误401.2 - 未经授权(对于DefaultDocument)

时间:2015-02-23 17:53:25

标签: c# asp.net visual-studio-2013 iis-8 default-document

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

  1. 在VS2013中,右键单击选择" New Project"
  2. 选择" ASP.NET Web应用程序",单击确定
  3. 选择"清空"项目,检查" Web表单"在底部,然后单击确定
  4. 右键单击该项目,然后选择"添加| Web Form" - 命名 Default.aspx with" Authentication Succeeded"作为页面内容
  5. 右键单击该项目,然后选择"添加| Web Form" - 命名 的Login.aspx
  6. 添加"登录"作为页面内容
  7. 分配"身份验证"设置" e.Authenticated的事件处理程序 = true"
  8. 更新下面列出的web.config
  9. 按F5
  10. 
    
    <?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>
    &#13;
    &#13;
    &#13;

    我看到的行为是http://localhost:12345/Default.aspx行为正常(始终)。换句话说,当我第一次转到Default.aspx时,它会将我重定向到Login.aspx页面。一旦我通过身份验证,我就可以看到Default.aspx页面。如果我注销并尝试再次转到Default.aspx页面,它会将我重定向到首先登录。

    然而,当我得到/ URL(没有Default.aspx)时,我收到401.2错误(即使我已经通过第一次验证)?

    Default.aspx页面被列为默认文档,如果我删除&#34;拒绝&#34;来自Web.Config的行 - 然后默认文档按预期运行。但是当Deny?在Web配置中列出,突然默认文档停止工作,我必须转到/Default.aspx以避免401.2错误。

    为什么会出现这样的行为?

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

1 个答案:

答案 0 :(得分:2)

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

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

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

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

相关问题