asp.net – Web.config允许特定用户的位置访问
我有一个网络服务器,用户可以从中下载特定于每个用户的文件.为确保每个用户只能下载自己的文件,他们必须通过基本身份验证进行身份验证.因此,对于每个用户,服务器上都有一个对用户特定文件夹具有读取权限的Windows帐户.
现在我想将此功能移动到另一台服务器.我不想为用户创建Windows帐户,但仍保留基本身份验证.所以我将Custom Basic Authentication HTTP Module与Custom MembershipProvider结合使用,让我可以在web.config中定义用户. 身份验证工作正常但在使用jack或jill登录后(请参阅web.config)我可以访问Dir1和Dir2这两个位置.如果我注释掉< allow users =“jack”/>也是如此.部分位置标签. 附加信息: <% Response.Write(HTTPContext.Current.User.Identity.Name) %> 根据登录的人员返回正确的用户名. <% Response.Write(HTTPContext.Current.User.Identity.IsAuthenticated) %> 返回True. 我有什么办法,只有杰克能够访问(=从Dir1下载文件),只有吉尔能够访问(=从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> 解决方法
更新#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 我实际上测试了它,它只允许插入dir1和jir in 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> 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="domainjack,domainjill"/> <deny users="*,?" /> <!-- make sure you deny anonymous with '?' --> </authorization> </system.web> 子目录web.config.确保明确拒绝所有其他用户.如果你不否认所有其他用户,他们仍然可以进入. <?xml version="1.0"?> <configuration> <system.web> <authorization> <allow users="domainjill" /> <deny users="*,?"/> <!-- explicitly deny all others,including anonymous --> </authorization> </system.web> </configuration> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 如何禁用通过IIS提供的单页面应用程序
- asp:Repeater 添加button 事件
- asp.net – 应用程序池循环如何影响ASP Net会话状
- asp.net-mvc-3 – 将AuthorizeAttribute应用于控
- asp.net – ASP菜单没有显示
- asp.net-mvc – 我们可以从现有数据库的选定表中
- 如何从Asp.Net核心身份验证中获取策略列表?
- asp.net-mvc – ASP.NET MVC:Action中的授权 –
- asp.net-mvc – MVC 2和MVC3有什么区别
- 处理asp:ScriptManager时,mscorlib.dll中的asp.