ASP.NET 安全角色和权限

发布于 2024-07-19 18:02:07 字数 323 浏览 6 评论 0原文

我对 ASP.NET 安全模型很满意,可以根据用户所处的角色允许/拒绝 web.config 中的用户访问,例如,

<system.web>
  <authorization>
    <allow roles = "Admin" />
  </authorization>
</system.web>

但是我想要做的是为管理员用户提供一组权限,这些权限可以然后进行检查,例如具有“可以打印文档”、“可以删除文档”等权限的管理员用户

这种事情是否可以开箱即用,或者我是否需要走自定义路线?

I'm comfortable with the ASP.NET security model whereby one can allow/deny access to users in the web.config based on what roles they are in e.g.

<system.web>
  <authorization>
    <allow roles = "Admin" />
  </authorization>
</system.web>

However what I want to do is give the admin user a set of permissions which can then be checked e.g. an Admin user with permissions like "can print documents", "can delete document"

Is this sort of thing possible out of the box or would I need to go down a custom route?

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

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

发布评论

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

评论(5

高冷爸爸 2024-07-26 18:02:07

您可以按照这篇 MSDN 文章中的说明使用 Azman。

但 Azman 有很多地方我不喜欢,因此我推出了自己的角色作为 RoleProvider 的补充(用于管理权限到角色映射的附加表、API 和管理工具)。

我的自定义实现非常简单:

  • 角色和权限之间的MN关系。

  • API“HasPermission”,用于测试给定主体是否具有给定权限。 这只是迭代所有角色并检查该角色是否具有给定的权限。 出于性能原因,映射权限角色使用 ASP.NET 缓存进行缓存。

You can use Azman as described in this MSDN article.

But there are a number of things I don't like about Azman, so I rolled my own as a complement to the RoleProvider (additional tables, APIs and admin tools that manage the mapping of permissions to roles).

My custom implementation is very simple:

  • M-N relationship between roles and permissions.

  • An API "HasPermission" that tests if a given principal has a given permission. This simply iterates through all roles and checks if the role has the given permission. The mapping permission-roles is cached using the ASP.NET cache for performance reasons.

请叫√我孤独 2024-07-26 18:02:07

它不是开箱即用的。 但如果您想要更细化,为什么不使用“CanPrint”、“CanDelete”等细化角色,而不是“Admin”等更广泛的角色呢?

如果他们想要一个容器类型场景,正如您在评论中指出的那样,您可以设置一个自定义 IPrincipal - 在身份验证之后,并且对于每个新请求,您都会查看用户的角色成员身份(“管理员”、“公共”等),然后覆盖您的 IPrincipal 上的 IsInRole。 您可以在此处找到示例

It's not there out of the box; but if you wanted to be more granular, why not have granular roles like "CanPrint", "CanDelete" rather than wider ones like "Admin"?

If they want a container type scenario as you indicate in your comments you could setup a custom IPrincipal - where, after authentication, and with each new request you look at the user's role membership ("Admin", "Public" etc.) and then override IsInRole on your IPrincipal. You can find an example here

萌逼全场 2024-07-26 18:02:07

我发现这篇文章举了一个很好的例子

[Flags]
public enum Permissions
{
View                 = (1 << 0),
Add                  = (1 << 1),
Edit                 = (1 << 2),
Delete               = (1 << 3),
Admin                = (View | Add | Edit | Delete)
}

public ActionResult Authenticate(string username, string password)
{
var user = authenticationService.Authenticate(username, password);
Session["User"] = user;

return RedirectToAction("Somewhere", "Else");  
}

public class PermissionsAttribute : ActionFilterAttribute
{
private readonly Permissions required;

public PermissionsAttribute(Permissions required)
{
    this.required = required;
}

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    var user = filterContext.HttpContext.Session.GetUser();
    if (user == null)
    {
        //send them off to the login page
        var url = new UrlHelper(filterContext.RequestContext);
        var loginUrl = url.Content("~/Home/Login");
        filterContext.HttpContext.Response.Redirect(loginUrl, true);   
    }
    else
    {
        if (!user.HasPermissions(required))
        {
            throw new AuthenticationException("You do not have the necessary permission to perform this action");
        }
    }
}
}

[Permissions(Permissions.View)]
public ActionResult Index()
{

// ...

}

i found this article that gives a nice example

[Flags]
public enum Permissions
{
View                 = (1 << 0),
Add                  = (1 << 1),
Edit                 = (1 << 2),
Delete               = (1 << 3),
Admin                = (View | Add | Edit | Delete)
}

public ActionResult Authenticate(string username, string password)
{
var user = authenticationService.Authenticate(username, password);
Session["User"] = user;

return RedirectToAction("Somewhere", "Else");  
}

public class PermissionsAttribute : ActionFilterAttribute
{
private readonly Permissions required;

public PermissionsAttribute(Permissions required)
{
    this.required = required;
}

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    var user = filterContext.HttpContext.Session.GetUser();
    if (user == null)
    {
        //send them off to the login page
        var url = new UrlHelper(filterContext.RequestContext);
        var loginUrl = url.Content("~/Home/Login");
        filterContext.HttpContext.Response.Redirect(loginUrl, true);   
    }
    else
    {
        if (!user.HasPermissions(required))
        {
            throw new AuthenticationException("You do not have the necessary permission to perform this action");
        }
    }
}
}

[Permissions(Permissions.View)]
public ActionResult Index()
{

// ...

}
朦胧时间 2024-07-26 18:02:07

您可以在 RoleProvider 中返回 PERMISSIONS 而不是 ROLES。

public override string[] GetRolesForUser(string username) {
   return GetGrantedPermissions(userName);
}

然后创建管理页面,向角色添加 {granted/denied} 权限,当然还有向角色添加用户权限。

You could return PERMISSIONS instead of the ROLES in your RoleProvider.

public override string[] GetRolesForUser(string username) {
   return GetGrantedPermissions(userName);
}

Then create your admin pages to add {granted/denied} permissions to roles and of course users to roles.

恰似旧人归 2024-07-26 18:02:07

是的,这是可能的。 创建所需的角色,将用户添加到角色,然后只需在执行需要该角色的操作的代码中检查 User.IsInRole 即可。

查看 System.Web.Security 中的 Roles 和 MemberShip 类

Yes it's possible. Create the roles you want, add the users to the roles, and then just check User.IsInRole in your code where you perform the action that requires that role.

Take a look at the Roles and MemberShip classes in System.Web.Security

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