asp.net文件夹授权

发布于 2024-08-22 00:52:26 字数 230 浏览 3 评论 0原文

我正在使用自己的数据库和表单身份验证。

该数据库包含一张包含用户的表和第二张包含用户分配到的角色的表。

问题是:如何在 web.config 中准备该部分,以便仅允许属于其中一个角色的用户访问该文件夹?

第二个问题: 使用 IIS 配置,我可以阻止对 Web 目录中所有文件夹的直接访问。假设其中一个页面包含允许从这些受保护的文件夹下载文件的链接。如果允许用户访问该网站,他是否也能够下载该内容?

I'm using my own database and forms authentication.

The database contains one table with users and second one with roles, that users are assigned to.

The question is: how to prepare the section in web.config, so it allows acces to the folder only for users belonging to one of the roles?

Second question:
Using IIS configuration I can block direct access to all folders in the web directory. Let's say, that one of pages will contain links allowing to download files from those protected folders. If user is allowed to acces that site will he also be able to download that content?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

·深蓝 2024-08-29 00:52:26

这是一个示例 web.config,如果您将此文件放置在一个文件夹中(在您的 Web 项目的结构内),您只想允许具有“管理员”角色的用户(例如),这将完成这项工作。

<?xml version="1.0"?>

<configuration>
    <appSettings/>
    <connectionStrings/>
    <system.web>
       <authorization>
          <allow roles="Admin"/>
          <deny users="*"/>
       </authorization>
    </system.web>
</configuration>

为了将其链接到您的安全性,在成功登录检查后,您需要创建一个 FormsAuthenticationTicket 并传递用户名和用户角色等详细信息。

一个简单的示例显示了这一点:

 FormsAuthenticationTicket myTicket = new FormsAuthenticationTicket(1, myUserName, DateTime.Now, DateTime.Now.AddMinutes(30), true, myUserRole, FormsAuthentication.FormsCookiePath);
 string hash = FormsAuthentication.Encrypt(myTicket);
 HttpCookie myCookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
 Response.Cookies.Add(myCookie);

这样您就可以在代码中执行此操作:

 if (Context.User.IsInRole("Admin")) {
      // Do Something
 } else {
      // Do Something Else
 }

并且您的 Web.config 文件将按照我上面详细说明的方式工作。

有关 FormsAuthenticationTickets 的更多信息,请参见此处 http://msdn.microsoft .com/en-us/library/system.web.security.formsauthenticationticket.aspx

Here is a sample web.config, if you placed this file within a folder (within the structure of your web project) where you only want to allow users with the "Admin" Role (for example) this will do the job.

<?xml version="1.0"?>

<configuration>
    <appSettings/>
    <connectionStrings/>
    <system.web>
       <authorization>
          <allow roles="Admin"/>
          <deny users="*"/>
       </authorization>
    </system.web>
</configuration>

In order to link this to your security, after a successful login check you need to create a FormsAuthenticationTicket and pass in details like the user name and user roles.

A simple example showing this is:

 FormsAuthenticationTicket myTicket = new FormsAuthenticationTicket(1, myUserName, DateTime.Now, DateTime.Now.AddMinutes(30), true, myUserRole, FormsAuthentication.FormsCookiePath);
 string hash = FormsAuthentication.Encrypt(myTicket);
 HttpCookie myCookie = new HttpCookie(FormsAuthentication.FormsCookieName, hash);
 Response.Cookies.Add(myCookie);

That way you can do this in your code:

 if (Context.User.IsInRole("Admin")) {
      // Do Something
 } else {
      // Do Something Else
 }

And your Web.config file will work as I detailed above.

More info on FormsAuthenticationTickets here http://msdn.microsoft.com/en-us/library/system.web.security.formsauthenticationticket.aspx

一袭白衣梦中忆 2024-08-29 00:52:26
<appSettings/>
<connectionStrings/>
<system.web>
   <authorization>
      <allow roles="Admin"/>
      <deny users="*"/>
   </authorization>
</system.web>

<appSettings/>
<connectionStrings/>
<system.web>
   <authorization>
      <allow roles="Admin"/>
      <deny users="*"/>
   </authorization>
</system.web>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文