【问题标题】:Authorising Web App users against User Information as well as Role针对用户信息和角色授权 Web 应用用户
【发布时间】:2011-04-30 17:01:51
【问题描述】:

我想知道是否有人可以帮助我解决以下问题?

我需要一些更复杂的 webapp 授权规则,而不仅仅是角色,我工作得很好。类似于“允许所有管理员。允许买家,前提是他们具有正确的部门 ID 并允许查看此客户的凭据”。

我正在使用自定义身份和自定义主体来存储信息,例如是否允许用户查看所有客户端或他们可以查看哪些单个客户端。此信息从数据库中检索并在创建身份/主体时添加。

我创建了一个扩展 IPermission 的自定义权限 ISecurityEncodable。在此,我将 Demand() 函数修改为以下内容:

public  void Demand()
    {
        this._identity = (UserIdentity)Thread.CurrentPrincipal.Identity;
        if (Thread.CurrentPrincipal.IsInRole("Admin")) { }
        else if ((Thread.CurrentPrincipal.IsInRole("Buyer")) && 
                 (this._identity.CanViewAllClients) &&
                 (this._identity.IsInDept(this._departmentID)) ) { }
        else if ((Thread.CurrentPrincipal.IsInRole("Buyer")) && 
                 (this._identity.CanViewClient(this._requestedClient)) &&
                 (this._identity.IsInDept(this._departmentID)) ) { }
        else { throw new SecurityException("Custom Permission Denied"); }  
    }

然后当我希望通过使用授权时调用它

CustomPermission custperm = new CustomPermission(requestedClient, reqClientDept);
custperm.Demand();

这很好用,但似乎是一种杂乱无章的做事方式。特别是因为将我的安全角色用作属性会很好,例如

[PrincipalPermission(SecurityAction.Demand, Authenticated = true)]
public class...

也许有一种方法可以使用自定义 IsAuthorised 检查调用 [CustomPrincipalPermission(SecurityAction.Demand, Authorized = true)]?这可能吗?需要实施什么?

如果我在网上错过了一个简单的解决方案,我深表歉意,但请放心,我已经检查了好几天了。

【问题讨论】:

    标签: c# .net security permissions authorization


    【解决方案1】:

    您想要的似乎是声明性需求而不是程序化需求。为此,您需要根据您的 CustomPermission 创建一个 CustomPermissionAttribute

    有一个自定义权限here的声明性需求示例以及创建自定义权限属性here的详细信息。

    【讨论】:

      猜你喜欢
      • 2011-09-29
      • 2021-08-30
      • 2015-03-19
      • 2018-02-09
      • 1970-01-01
      • 2011-12-15
      • 2021-08-26
      • 1970-01-01
      • 2015-11-17
      相关资源
      最近更新 更多