业务层基于角色的授权
Role based authorization in Business layer
我有一个 3 层应用程序 - 1) UI 层是一个 ASP .NET MVC 应用程序 2) 业务层是一个 class 库 3) 数据访问layer 是一个 class 库。我使用基于声明的基于角色的授权。我在 Application_AuthenticateRequest 事件处理程序
中设置声明
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
var authenticationCookie =
HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
var ticket = FormsAuthentication.Decrypt(authenticationCookie.Value);
FormsIdentity formsIdentity = new FormsIdentity(ticket);
ClaimsIdentity claimsIdentity = new ClaimsIdentity(formsIdentity);
// Get the roles from database
...
var role = GetUserRole();
claimsIdentity.AddClaim(new Claim(ClaimTypes.Role, role));
ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
Thread.CurrentPrincipal = claimsPrincipal;
}
现在我有两种方式可以访问业务层的角色
1) 从线程主体直接访问角色
var principal = Thread.CurrentPrincipal as ClaimsPrincipal;
var claim = principal.Claims.FirstOrDefault(x => x.Type ==
ClaimTypes.Role);
优点:
- 每个方法都可以隐式访问角色。
缺点
- 难以进行单元测试,因为它依赖于静态对象
2) 将角色作为参数传递给需要它的方法,例如
public IUserService {
void CreateUser(User user, string role);
}
优点
- 易于单元测试,因为角色明确传递给方法
缺点
- 每个方法都需要有一个参数
- 如果将授权从基于角色的授权更改为任何形式的授权,则会破坏系统
还有什么选择?在业务层实现基于角色授权的标准方式是什么?
有一种标准方法可以跨多个层实施外部化、细粒度授权(不仅仅是基于角色)。该模型称为 abac and stands for attribute based access control. There is one major framework which implements it and that's called xacml。另一种选择是 .Net 中基于声明的授权。
ABAC/XACML定义:
- 用于定义授权规则的策略语言,例如
a manager can view documents they own
.
一方面定义的架构
一个。处理授权请求并生成决策的策略决策点 (PDP) (Permit/Deny)
b。策略执行点 (PEP),它保护您的应用程序、拦截业务请求并将授权请求发送到 PDP。这是一段代码,可以作为过滤器、拦截器、注释或更多...
使用外部化授权的好方法是您可以一次跨多个层应用相同的一致授权(从 Web SSO 到业务层甚至数据层)。
HTH。维基百科上有很多资源。您也可以查看此 blog 我尽量保持最新状态。
我有一个 3 层应用程序 - 1) UI 层是一个 ASP .NET MVC 应用程序 2) 业务层是一个 class 库 3) 数据访问layer 是一个 class 库。我使用基于声明的基于角色的授权。我在 Application_AuthenticateRequest 事件处理程序
中设置声明protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
var authenticationCookie =
HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
var ticket = FormsAuthentication.Decrypt(authenticationCookie.Value);
FormsIdentity formsIdentity = new FormsIdentity(ticket);
ClaimsIdentity claimsIdentity = new ClaimsIdentity(formsIdentity);
// Get the roles from database
...
var role = GetUserRole();
claimsIdentity.AddClaim(new Claim(ClaimTypes.Role, role));
ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
Thread.CurrentPrincipal = claimsPrincipal;
}
现在我有两种方式可以访问业务层的角色
1) 从线程主体直接访问角色
var principal = Thread.CurrentPrincipal as ClaimsPrincipal;
var claim = principal.Claims.FirstOrDefault(x => x.Type ==
ClaimTypes.Role);
优点:
- 每个方法都可以隐式访问角色。
缺点
- 难以进行单元测试,因为它依赖于静态对象
2) 将角色作为参数传递给需要它的方法,例如
public IUserService {
void CreateUser(User user, string role);
}
优点
- 易于单元测试,因为角色明确传递给方法
缺点
- 每个方法都需要有一个参数
- 如果将授权从基于角色的授权更改为任何形式的授权,则会破坏系统
还有什么选择?在业务层实现基于角色授权的标准方式是什么?
有一种标准方法可以跨多个层实施外部化、细粒度授权(不仅仅是基于角色)。该模型称为 abac and stands for attribute based access control. There is one major framework which implements it and that's called xacml。另一种选择是 .Net 中基于声明的授权。
ABAC/XACML定义:
- 用于定义授权规则的策略语言,例如
a manager can view documents they own
. 一方面定义的架构
一个。处理授权请求并生成决策的策略决策点 (PDP) (Permit/Deny)
b。策略执行点 (PEP),它保护您的应用程序、拦截业务请求并将授权请求发送到 PDP。这是一段代码,可以作为过滤器、拦截器、注释或更多...
使用外部化授权的好方法是您可以一次跨多个层应用相同的一致授权(从 Web SSO 到业务层甚至数据层)。
HTH。维基百科上有很多资源。您也可以查看此 blog 我尽量保持最新状态。