更新#3
您可以启用 URLAuthorization 以强制 IIS 保护 IIS 中通常不处理的文件。这里的解决方案依赖于 IIS 7.x 并使用集成管道。
<system.webServer>
<modules>
<add name="FormsAuthenticationModule" type="System.Web.Security.FormsAuthenticationModule" />
<remove name="UrlAuthorization" />
<add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
<remove name="DefaultAuthentication" />
<add name="DefaultAuthentication" type="System.Web.Security.DefaultAuthenticationModule" />
</modules>
</system.webServer>
更新#2您只需删除已添加的自定义内容并执行以下操作即可完全切换到表单身份验证。
我实际上测试过这个,它只允许jack in to dir1 and jill in dir2。两者都可以访问根目录。
如果这不起作用,我们将需要讨论您的更多设置。
网络配置
<?xml version="1.0"?>
<configuration>
<system.webServer>
<modules>
<add name="FormsAuthenticationModule" type="System.Web.Security.FormsAuthenticationModule" />
<remove name="UrlAuthorization" />
<add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
<remove name="DefaultAuthentication" />
<add name="DefaultAuthentication" type="System.Web.Security.DefaultAuthenticationModule" />
</modules>
</system.webServer>
<system.web>
<authentication mode="Forms">
<forms loginUrl="Login.aspx" defaultUrl="Default.aspx">
<credentials passwordFormat="Clear">
<user name="jack" password="jack" />
<user name="jill" password="jill" />
</credentials>
</forms>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
<compilation debug="true"></compilation>
<customErrors mode="Off"/>
</system.web>
<location path="dir1">
<system.web>
<authorization>
<allow users="jack" />
<deny users="*, ?" />
</authorization>
</system.web>
</location>
<location path="dir2">
<system.web>
<authorization>
<allow users="jill" />
<deny users="*, ?" />
</authorization>
</system.web>
</location>
</configuration>
登录.aspx- 您必须添加来自登录控件的重定向,否则表单身份验证将在 App_Code 目录中查找不存在的数据库。
<asp:Login ID="Login1" runat="server" OnAuthenticate="Login1_Authenticate">
</asp:Login>
登录.aspx.cs
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string username = Login1.UserName;
string password = Login1.Password;
if (FormsAuthentication.Authenticate(username, password))
{
FormsAuthentication.RedirectFromLoginPage(username, false);
}
}
更新#1
我浏览了您作为自定义基本身份验证 HTTP 模块链接的示例,然后继续执行HTTP 模块它的最底部有一个指向其他来源的链接。
该源有一个使用自定义基本身份验证的会员提供程序示例。我觉得您在 web.config 中混入 Forms 会员资格提供程序时遇到了麻烦。
当您开始进行自己的单独身份验证时,事情进展并不顺利,您通常需要添加自己的所有内容。
这段代码可以从我这边的附加链接开始工作。
作为一种额外的可能性,如果您想让 ASP.NET 本身处理所有成员资格并且您使用 SQL 来存储所有内容,请考虑查看http://weblogs.asp.net/sukumarraju/archive/2009/10/02/installing-asp-net-membership-services-database-in-sql-server-expreess.aspx查看如何使用向导在 SQL 中进行设置。
内置的成员身份将是表单身份验证,并且比使用自定义身份验证要少得多。
以前的版本
我从来没有幸运地使用过<location>
标签,所以我只是将新的 web.configs 放入目录中。当我不在子文件夹中排除匿名时,我也遇到了麻烦。这似乎是浏览器默认为匿名,可以通过
我是这样做的。
根 web.config
<system.web>
<authorization>
<allow roles="AccessRole1, AccessRole2" users="domain\jack, domain\jill"/>
<deny users="*, ?" /> <!-- make sure you deny anonymous with '?' -->
</authorization>
</system.web>
子目录web.config。确保您明确拒绝所有其他用户。如果您不拒绝所有其他用户,他们仍然可以进入。
<?xml version="1.0"?>
<configuration>
<system.web>
<authorization>
<allow users="domain\jill" />
<deny users="*, ?"/> <!-- explicitly deny all others, including anonymous -->
</authorization>
</system.web>
</configuration>