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

Phi*_*ohl 10 asp.net permissions web-config

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

现在我想将此功能移动到另一台服务器.我不想为用户创建Windows帐户,但仍保留基本身份验证.因此,我将自定义基本身份验证HTTP模块自定义成员资格提供程序结合使用,后者允许我在web.config中定义用户.

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

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

<% Response.Write(HTTPContext.Current.User.Identity.Name) %>
Run Code Online (Sandbox Code Playgroud)

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

<% Response.Write(HTTPContext.Current.User.Identity.IsAuthenticated) %>
Run Code Online (Sandbox Code Playgroud)

返回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>
Run Code Online (Sandbox Code Playgroud)

Kir*_*irk 9

更新#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>
Run Code Online (Sandbox Code Playgroud)

更新#2 您只能通过删除已添加的自定义内容完全切换到Forms身份验证,然后执行以下操作.

其实我已经测试了这一点,并只允许插孔DIR1吉尔DIR2.两者都可以访问root.

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

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>
Run Code Online (Sandbox Code Playgroud)

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

<asp:Login ID="Login1" runat="server" OnAuthenticate="Login1_Authenticate">
</asp:Login>
Run Code Online (Sandbox Code Playgroud)

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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

更新#1

我浏览了您作为自定义基本身份验证HTTP模块链接的示例,然后跟随到HTTP模块,该模块最底层的链接指向其他源.

此源具有使用自定义基本身份验证的成员资格提供程序示例 我觉得你通过混合web.config中的Forms成员资格提供程序来解决问题.

当您开始进行单独的身份验证时,事情并不顺利,您通常需要添加自己的所有内容.

这段代码可以从我最后的附加链接开始.

作为一种额外的可能性,如果您想让ASP.NET处理所有成员身份并且您使用SQL来存储所有内容,请考虑查看http://weblogs.asp.net/sukumarraju/archive/2009/10/ 02/installation-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>
Run Code Online (Sandbox Code Playgroud)

子目录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>
Run Code Online (Sandbox Code Playgroud)