Authentication 在子文件夹或虚拟目录上禁用窗体身份验证

Authentication 在子文件夹或虚拟目录上禁用窗体身份验证,authentication,iis,forms-authentication,Authentication,Iis,Forms Authentication,以下是我的场景: <?xml version="1.0"?> <configuration> <connectionStrings> <add name="MyConnString" connectionString="Persist Security Info=True;Initial Catalog=MyDB;Data Source=MyServer;User ID=UserName;Pa

以下是我的场景:

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="MyConnString" connectionString="Persist Security Info=True;Initial Catalog=MyDB;Data Source=MyServer;User ID=UserName;Password=xxxxxxxxxx;MultipleActiveResultSets=True;"/>
  </connectionStrings>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>
  </system.web>
  <system.webServer>
    <modules runManagedModulesForWebDavRequests="true" runAllManagedModulesForAllRequests="true">
      <add name="CustomWebDAVModule" type="CustomWebDAVModule"/>
    </modules>
  </system.webServer>
</configuration>
我已经在IIS(7.5)中建立了一个使用表单身份验证的网站。我在该网站中有一个子文件夹,用作WebDAV共享。我有一个自定义HTTP模块来监视我的WebDAV请求,还充当一个自定义身份验证级别。当用户尝试将驱动器映射到my WebDAV共享时,此自定义身份验证将首先发送HTTP 401质询以获取用户的凭据,然后从基本身份验证头服务器端解析凭据。问题是,只有在关闭表单身份验证时,才会发送基本身份验证标头

更重要的是,通常当我的HTTP模块找不到Auth头时,会发送401质询(当Forms Auth关闭时,会提示用户输入凭据)。但是,在启用Forms Auth的情况下,我的HTTP模块仍然执行并发送401质询,但Forms Auth似乎具有优先级,因此在Fiddler中,我可以清楚地看到重定向到:

/Account/Login.aspx?ReturnURL=MySubFolder

自定义身份验证的要点是,当将驱动器映射到我的WebDAV共享时,我可以允许用户登录到我的站点。我想捕获他们的网站凭据,对他们进行身份验证,然后向他们显示目录的内容

所以我的问题是:

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="MyConnString" connectionString="Persist Security Info=True;Initial Catalog=MyDB;Data Source=MyServer;User ID=UserName;Password=xxxxxxxxxx;MultipleActiveResultSets=True;"/>
  </connectionStrings>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>
  </system.web>
  <system.webServer>
    <modules runManagedModulesForWebDavRequests="true" runAllManagedModulesForAllRequests="true">
      <add name="CustomWebDAVModule" type="CustomWebDAVModule"/>
    </modules>
  </system.webServer>
</configuration>
在启用了表单身份验证的网站中,是否有方法在子文件夹或虚拟目录上禁用表单身份验证

我已经验证了我可以通过在我的网站中创建一个新的应用程序并将子文件夹放在其中,然后在应用程序本身上禁用Forms Auth来解决这个问题,但是如果可能的话,我真的不希望这样做

我尝试的一切(如下所列)都导致我请求将驱动器映射到
Http://localhost/MySubFolder
被表单身份验证接管(至少我认为是这样),并重定向到
/login.aspx?ReturnUrl=MySubFolder
(如Fiddler中所示)

以下是我尝试过的:

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="MyConnString" connectionString="Persist Security Info=True;Initial Catalog=MyDB;Data Source=MyServer;User ID=UserName;Password=xxxxxxxxxx;MultipleActiveResultSets=True;"/>
  </connectionStrings>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>
  </system.web>
  <system.webServer>
    <modules runManagedModulesForWebDavRequests="true" runAllManagedModulesForAllRequests="true">
      <add name="CustomWebDAVModule" type="CustomWebDAVModule"/>
    </modules>
  </system.webServer>
</configuration>
1)
MySubFolder
中添加了一个单独的Web.config:

   <configuration>
     <system.web>
       <authorization>
         <allow users="*"/>
       </authorization>
     </system.web>
   </configuration>
3)查看了IIS中的更新

就我个人而言,我对上述解决方案有些怀疑,因为从我所读到的内容来看,它们只是允许所有访问,同时仍然启用表单身份验证我需要一种方法在我的子文件夹上禁用表单身份验证。这可能吗

我还应该注意,我的子文件夹可以是一个虚拟目录,但它不是必需的。我只需要一种方法,使窗体身份验证在该文件夹上被禁用

根据请求,我的Web.config文件(站点级):

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="MyConnString" connectionString="Persist Security Info=True;Initial Catalog=MyDB;Data Source=MyServer;User ID=UserName;Password=xxxxxxxxxx;MultipleActiveResultSets=True;"/>
  </connectionStrings>
  <system.web>
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>
  </system.web>
  <system.webServer>
    <modules runManagedModulesForWebDavRequests="true" runAllManagedModulesForAllRequests="true">
      <add name="CustomWebDAVModule" type="CustomWebDAVModule"/>
    </modules>
  </system.webServer>
</configuration>

显然,您没有在IIS中正确设置WebDAV

由于您希望使用自定义webdav模块进行身份验证,我们首先需要确保IIS不会干扰您的webdav请求

遵循以下步骤(请注意,重要的一点在步骤7中):

  • 保持你的
    web.config
    原样(如问题中所述)

  • 在IIS中启用WebDav。在IIS管理器中选择您的网站。在主窗口中打开
    WebDAV创作规则

  • 在操作窗格(右侧)中,单击
    启用WebDAV

  • 现在,单击要为其启用WebDAV的子文件夹(我在示例中使用的是
    path3
    ),然后打开
    WebDAV创作规则

  • 单击操作窗格(右侧)中的添加创作规则…

  • 在打开的对话框中,确保所有这些选项都已选中
    所有内容
    所有用户
    读取
    源代码
    写入
    ,然后单击
    确定
    。这将授予具有所有权限的所有用户访问该子文件夹的所有内容的权限

  • 现在是最重要的部分。由于您希望通过自定义WebDAV处理程序处理WebDAV身份验证,因此我们必须明确告知IIS不要干扰身份验证。我们怎么做?我们告诉IIS允许匿名WebDAV访问。通过这种方式,IIS不会尝试对用户进行身份验证,您的模块可以自由执行其授权操作。为此,我们需要在
    WebDav设置下将
    允许匿名属性查询设置为
    True
    。接下来的步骤将重点介绍如何做到这一点

  • 打开网站根
    WebDAV创作规则

  • 单击
    WebDAV设置…

  • 允许匿名属性查询设置为
    True

  • 关闭并重新启动将在其上进行测试的WebDAV客户端。这是为了确保它不会缓存以前错误配置中的连接参数

  • 检查您网站的WebDAV配置是否按要求工作。:)


  • 这个问题已经得到了回答:

    您不能覆盖位置标记中的root authentication mode=“Forms”标记。使文件夹成为自己的应用程序是最简单的方法

    另一个选项是实现您自己的自定义表单身份验证,并让它忽略webdav文件夹的重定向

    <authentication mode="None">
    

    在您的自定义HTTP模块中,您可以通过
    HttpResponse.SuppressFormsAuthenticationRedirect告诉表单身份验证抑制重定向:

    public class DavAuthenticationModule : IHttpModule
    {
            public void Init(HttpApplication application)
            {
                application.AuthenticateRequest += App_OnAuthenticateRequest;
            }
    
            private void App_OnAuthenticateRequest(object source, EventArgs eventArgs)
            {
                // Only applies for WebDAV requests.
                var ctx = HttpContext.Current;
                if (!ctx.Request.Path.StartsWith("/dav/path", StringComparison.OrdinalIgnoreCase))
                    return;
    
                // So that forms auth won't do a redirect.
                // Note that it will still attempt to read / parse the forms auth cookie.
                ctx.Response.SuppressFormsAuthenticationRedirect = true;        
    
                // Now do my own auth.
                DoBasicHttpAuthentication(ctx);
            }
    }
    
    另一个选项是挂接
    FormsAuthenticationModule.Authenticate
    事件,以便在Forms Auth运行之前进行身份验证。这需要您在IIS中查找模块实例,我没有示例

    都是t