2012-04-03 55 views
9

我有一个Web服务器,用户可以在其中下载针对每个用户特定的文件。为确保每个用户只能下载自己的文件,他们必须通过基本认证进行认证。因此,对于每个用户,服务器上都有一个windows用户帐户,具有对用户特定文件夹的读取权限。Web.config允许特定用户的位置访问

现在我想将此功能移到另一台服务器。我不想为用户创建Windows帐户,但仍保留基本身份验证。所以我使用Custom Basic Authentication HTTP Module结合Custom 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> 
+1

我需要一些时间进行我的研究工作。将尽快更新。 – Pankaj 2012-05-02 06:21:44

+0

**我可以访问位置Dir1和Dir2 **您的Web服务器IIS中是否启用了目录浏览功能? – Pankaj 2012-05-02 14:30:10

+0

不,我没有启用目录浏览。我的意思是我可以查看/下载这些目录中包含的文件,而不管用户登录的是什么。 – 2012-05-02 15:29:34

回答

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身份验证,并做到以下几点。

我实际测试过这一点,并只允许插孔DIR1吉尔DIR2。两者都可以访问根目录。

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

的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 - 你必须在重定向从登录控件添加,否则表单身份验证将寻找在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。当我不排除子文件夹中的匿名时,我也遇到了麻烦。这似乎是浏览器将默认为匿名将通过

这是我如何做到这一点。

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

Sub目录的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> 
+0

我没有角色或域用户。我试图排除匿名用户,但我仍然有同样的问题。我认为问题可能在自定义MembershipProvider和/或CustomAuthentication的某处。 – 2012-05-02 09:36:58

+0

你能提供你的会员资格/验证码的详细信息吗?我在过去创建了一些简单的应用程序,可能会有所帮助。 – Kirk 2012-05-02 16:23:46

+0

正如问题中提到的,我使用了以下的MembershipProvider:http://www.leastprivilege.com/PermaLink.aspx?guid=628c9d74-9039-4465-8533-e821506aa9a2扩展名为http://www.leastprivilege。 COM/ASPNETMembershipProviderForWebconfig2ndTry.aspx与HTTP模块结合使用。请让我知道你需要什么信息。谢谢 – 2012-05-02 16:45:30

2

这里是与细节的几种情况,一个很好的文章的链接,其中一个将要允许/拒绝访问特定网页或文件夹:

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

作为边评论,在一个项目中,我们做的,我们使用单独的web.config文件的选项,每个文件夹中,如在规定的链接,以及,它为我们工作就好了。

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

+0

谢谢你的回答。我像你说的那样为子目录添加了独立的web.config文件。但问题是一样的。每个用户仍然可以访问每个子目录。 – 2012-04-03 11:13:44

0

使用此一步一步的指导到特定的文件和文件夹。

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

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");

或用户一些用户友好的消息发送到一些其他页面。

相关问题