【发布时间】: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