[英]C# Role-based security
I have a class library which contains my data base access layer, and i use it in all my projects which works with this DB, now i want to integrate security in this library so i can return different data for different security roles. 我有一个包含我的数据库访问层的类库,我在所有与这个数据库一起工作的项目中使用它,现在我想在这个库中集成安全性,这样我就可以为不同的安全角色返回不同的数据。 What is the best way to achieve this with .NET build-in security?
使用.NET内置安全性实现此目的的最佳方法是什么? I was thinking about using System.Security.Permissions.PrincipalPermission but i can't see how it can help me out, because anyone using my library can write client application like this
我正在考虑使用System.Security.Permissions.PrincipalPermission但我看不出它如何帮助我,因为任何使用我的库的人都可以编写像这样的客户端应用程序
GenericIdentity genericIdentity = new GenericIdentity("User");
GenericPrincipal genericPrincipal = new GenericPrincipal(genericIdentity, new[] { "Administrator" });
Thread.CurrentPrincipal = genericPrincipal;
And they will pass all my principal permission demands 他们将通过我所有的主要许可要求
PrincipalPermission principalPermission = new PrincipalPermission(null, "Administrator");
principalPermission.Demand();
without even authenticating. 甚至没有验证。 Either i don't understand this security model, or it just doesn't secure anything.
要么我不理解这个安全模型,要么它只是不保护任何东西。
Role-based security is intended as a way for library code to consume the client's chosen security model without needing to know the implementation; 基于角色的安全性旨在使库代码能够使用客户端选择的安全模型,而无需了解实现; the key here being that you are already at a point where it is reasonably to trust the client's security model, and you just want to offer appropriate options based on decisions made by the client (for example as part of a login process).
关键在于您已经处于合理地信任客户端安全模型的位置,并且您只是想根据客户端做出的决策提供适当的选项(例如,作为登录过程的一部分)。
If you don't trust the client, all bets are off anyway , as they could just use reflection to rip your internals to shreds. 如果你不信任客户端,所有的赌注都关闭, 无论如何 ,因为他们可以只使用反射来撕裂你的内部撕成碎片。 In that scenario, it would be better to keep that implementation code private, for example via a web-service that accepts the user's credentials in some fashion.
在这种情况下,最好将该实现代码保密,例如通过以某种方式接受用户凭据的Web服务。 Then they can only be accounts that they have the credentials for.
然后,他们只能是他们拥有凭据的帐户。
public class Example
{
// Will enforce that the user have the administrator role
[PrincipalPermission(XXXX, Role="Administrator")]
public void Remove(int userId)
{
}
public void Add(int userId)
{
if (!Thread.CurrentPrincipal.IsInRole("User"))
throw new UnauthorizedAccessException("Only registered users may add users");
}
}
How the actual principal/identity configuration is setup is up to the user of your library. 如何设置实际的主体/身份配置取决于您的库的用户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.