Web.config允许特定用户进行位置访问

时间:2012-04-03 09:43:41

标签: asp.net permissions web-config

我有一个网络服务器,用户可以从中下载特定于每个用户的文件。为确保每个用户只能下载自己的文件,他们必须通过基本身份验证进行身份验证。因此,对于每个用户,服务器上都有一个对用户特定文件夹具有读取权限的Windows帐户。

现在我想将此功能移至另一台服务器。我不想为用户创建Windows帐户,但仍保留基本身份验证。因此,我将Custom Basic Authentication HTTP ModuleCustom MembershipProvider结合使用,以便我在web.config中定义用户。

身份验证工作正常但在使用jackjill登录后(请参阅web.config)我可以访问Dir1Dir2这两个位置。如果我在位置标记中注释掉<allow users="jack" />部分,也会出现这种情况。

其他信息: 我创建了一个Default.aspx文件并添加了一个

<% Response.Write(HTTPContext.Current.User.Identity.Name) %>

根据登录的人员返回正确的用户名。

<% Response.Write(HTTPContext.Current.User.Identity.IsAuthenticated) %>

返回True。

我必须做的是,只有jack才能访问{=从Dir1下载文件,只有jill能够访问(=从中下载文件) )Dir2但不是相反?

编辑:我尝试为每个子目录添加web.config文件,而不是utkai提到的位置标记 - 具有相同的结果。每个用户都可以访问任何目录。

这是我的Web.config文件:

<configuration>
<system.webServer>
    <modules>
        <add name="CustomBasicAuthentication" type="LeastPrivilege.CustomBasicAuthentication.CustomBasicAuthenticationModule, LeastPrivilege.CustomBasicAuthenticationModule, Version=1.0.0.0, Culture=neutral, PublicKeyToken=F20DC168DFD54966"/>
    </modules>

    <security>
        <authentication>
            <customBasicAuthentication enabled="true" realm="TEST" providerName="AspNetWebConfigMembershipProvider" cachingEnabled="true" cachingDuration="15" requireSSL="false"/>
        </authentication>
        <authorization>
            <deny users="?" />
        </authorization>
    </security>
</system.webServer>

<system.web>
    <membership defaultProvider="AspNetWebConfigMembershipProvider">
        <providers>
            <add name="AspNetWebConfigMembershipProvider" type="LeastPrivilege.AspNetSecurity.Samples.WebConfigMembershipProvider, WebConfigMembershipProvider"/>
        </providers>
    </membership>

    <authentication mode="Forms">
        <forms>
            <credentials passwordFormat="Clear">
                <user name="jack" password="jack"/>
                <user name="jill" password="jill"/>
            </credentials>
        </forms>
    </authentication>

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

<location path="Dir1" allowOverride="false">
    <system.web>
        <authorization>
            <!-- <allow users="jack" /> -->
            <deny users="*" />
        </authorization> 
    </system.web>
</location>

<location path="Dir2"  allowOverride="false">
    <system.web>
        <authorization>
            <!-- <allow users="jill" /> -->
            <deny users="*" />
        </authorization> 
    </system.web>
</location>
</configuration>

5 个答案:

答案 0 :(得分:8)

更新#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 您只能通过删除已添加的自定义内容完全切换到Forms身份验证,然后执行以下操作。

我实际测试了这一点,它只允许 jack 进入 dir1 dir2 中的 jill 。两者都可以访问根目录。

如果这不起作用,我们需要讨论更多您的设置。

<强>的web.config

<?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>

Login.aspx - 您必须从Login控件添加重定向,否则Forms身份验证将在App_Code目录中查找不存在的数据库。

<asp:Login ID="Login1" runat="server" OnAuthenticate="Login1_Authenticate">
</asp:Login>

<强> 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模块进行链接的示例,然后进入The HTTP Module,其中包含最底层链接到其他来源。

此源具有使用自定义基本身份验证的成员资格提供程序示例。我觉得你通过混合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。当我不在子文件夹中排除匿名时,我也遇到了麻烦。这似乎是浏览器将默认为匿名,将通过

我是这样做的。

Root 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>

答案 1 :(得分:2)

这是一篇很好的文章的链接,其中包含几种情况的详细信息,其中一个人想要允许/拒绝访问特定页面或文件夹:

Setting authorization rules for a particular page or folder in web.config

作为旁注,我们在一个项目中,我们在每个文件夹中使用单独的web.config文件选项,如链接中所述,它对我们很有用。

希望它有助于解决您的问题。

答案 2 :(得分:0)

这种方法类似但不同 - 位置是文件而不是目录:

Is it possible to allow anonymous user to browse only few files from a folder

答案 3 :(得分:0)

使用此分步指南将标记应用于Web.config文件,以配置对特定文件和文件夹的访问。

<location path="default1.aspx">
    <system.web>
        <authorization>
            <allow users ="*" />
        </authorization>
    </system.web>
</location>
<!-- This section gives the unauthenticated user access to all of the files that are stored in the Subdir1 folder.  -->
<location path="subdir1">
    <system.web>
        <authorization>
            <allow users="Admin" />
        </authorization>
    </system.web>
</location>

More info...

答案 4 :(得分:0)

Web.config

中设置以下内容

<modules runAllManagedModulesForAllRequests="false">

将以下事件放入Global.asax文件中。

protected void Application_BeginRequest(Object sender, EventArgs e)
{
}

现在每当你输入如下的URl时。

  

http://localhost/dir1/jack.txt

控件将始终移至Application_BeginRequest事件。您有Request.Url个信息和Current User information,您可以在此处进行验证。

使用以下代码

throw new HttpException(403,"Acess Denied");

或将用户发送到另一个页面,其中包含一些用户友好的消息。