2

我的 asp.net webform 应用程序中有以下文件夹结构。

_AdminUser
_ModeratorUser
_EmployeeUser
Images
js
css
ckeditor
App_Code
errorPages
Default.aspx
News.aspx
Article.aspx

到目前为止,我只有一种类型的用户曾经编辑过网站的内容。我曾经简单地授权用户并将授权用户重定向到文件夹'_AdminUser',以便他们

可以对网站进行更改。

下面 web.config 中的代码足以让我毫无问题地工作。

    <authentication mode="Forms">
      <forms loginUrl="~/_Login.aspx" timeout="2880"/>
    </authentication>

  <location path="_adminUser">
    <system.web>
      <authorization>
        <deny users="?"/>
      </authorization>
    </system.web>
  </location>
  <location path="ckeditor">
    <system.web>
      <authorization>
        <deny users="?"/>
      </authorization>
    </system.web>
  </location>

在我的新项目中,我必须创建三种不同类型的用户

  1. 管理员用户(超级用户并且可以访问不同文件夹中的所有文件)
  2. 版主用户(此类用户只能访问此文件夹_ModeratorUser和其他常规文件夹中的文件,但不能访问_AdminUser_EmployeeUser
  3. 员工用户(此类用户只能访问此文件夹中的文件 _EmployeeUser & other general folder but no access to_AdminUser or_EmployeeUser` )

为了实现这一点,我创建了三种类型的角色AdminModerator并且Employee。当我创建新用户时,我将其分配给特定角色,并且我希望每个角色都可以访问如上所述的不同文件夹。

但我不确定如何修改 web.config 文件,以便获得这种基于角色的权限。我一直在寻找这样的教程,但到目前为止还没有运气。我看过的其他教程似乎没有解决我的问题。我将不胜感激指向正确方向的指针。

4

3 回答 3

3

我假设您使用 ASP.Net 成员资格和角色提供程序。如果是这样,您需要在每个文件夹中使用单独的 web.config 来限制权限。

Admin 文件夹内的 web.config

以下 web.conf 设置(位于 Admin 文件夹内)仅允许Admin角色用户访问 Admin 文件夹内的文件。其他用户无法访问这些文件。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.web>
    <authorization>
      <allow roles="Admin" />
      <deny users="*" />
    </authorization>
  </system.web>
</configuration>
于 2013-07-24T23:14:24.623 回答
0

Web.Config 设置

  <!--Path: folder path -->
  <location path="_adminUser">
    <system.web>
      <authorization>
        <!-- Allow user who have Admin role can access the AdminUser folder aspx pages -->
        <allow roles="Admin"/>
        <!-- Other user can not access AdminUser folder aspx pages -->
        <deny users="*"/>
      </authorization>
    </system.web>
  </location>

  <!--Path: folder path -->
  <location path="_EmployeeUser">
    <system.web>
      <authorization>
        <!-- Allow user who have Client role can access the ClientUser folder aspx pages -->
        <allow roles="Employee"/>
        <!-- Other user can not access ClientUser folder aspx pages -->
        <deny users="*"/>
      </authorization>
    </system.web>
  </location>

我还为我的登录控制按钮编写了以下代码

protected void Login1_LoggedIn(object sender, EventArgs e) { // 请不要在这里使用 User.IsInRole ,因为在这个阶段它还不会被填充。

if (Roles.IsUserInRole(Login1.UserName, "Admin"))
{
    Login1.DestinationPageUrl = "~/_adminUser/Default.aspx";
}
else if (Roles.IsUserInRole(Login1.UserName, "Heroes"))
{
    Login1.DestinationPageUrl = "~/_EmployeeUser/Default.aspx";
    }
}

上述方法对我有用,但它有一个缺点,即每次添加新角色时我都必须编写代码。我不确定哪个单独的 web.config 文件无法正常工作,正如其中一个解决方案中提到的那样。

于 2013-07-25T05:59:31.590 回答
0

这里有一个非常好的教程,您甚至可以下载一个用于管理用户、角色、权限的工具。您可以从页面底部的附件部分下载源代码。

于 2013-07-24T06:45:10.437 回答