c# - .NET 中的身份验证、授权、用户和角色管理以及一般安全性

标签 c# security authorization roles

我需要知道如何为 C# 应用程序实现一般安全性。在这方面我有什么选择?如果现有框架满足我的需求,我更愿意使用它 - 我不想重新发明轮子。

我的要求如下:

  • 通常的用户名/密码验证
  • 管理用户 - 为用户分配权限
  • 管理角色 - 将用户分配给角色,将权限分配给角色
  • 根据用户名和角色对用户进行授权

我正在寻找一个免费/开源的框架/库,它已经过时并被 .Net 社区使用。

我的应用程序采用客户端/服务器方法,服务器作为 Windows 服务运行,连接到 SQL Server 数据库。客户端和服务器之间的通信将通过 WCF。

另一件重要的事情是我需要能够为特定用户或角色分配权限以查看/更新/删除特定实体,无论是客户还是产品等。例如Jack 可以查看 10 个客户中的某个 3 个,但只能更新客户 Microsoft、Yahoo 和 Google 的详细信息,并且只能删除 Yahoo。

最佳答案

对于粗粒度的安全性,您可能会发现内置主体代码很有用;用户对象(及其角色)在 .NET 中由“主体”控制,但有用的是,运行时本身可以强制执行此操作。

主体的实现可以是实现定义的,您通常可以注入(inject)自己的; for example in WCF .

要查看执行粗略访问的运行时(即可以访问哪些功能,但不限于哪些特定数据):

static class Roles {
    public const string Administrator = "ADMIN";
}
static class Program {
    static void Main() {
        Thread.CurrentPrincipal = new GenericPrincipal(
            new GenericIdentity("Fred"), new string[] { Roles.Administrator });
        DeleteDatabase(); // fine
        Thread.CurrentPrincipal = new GenericPrincipal(
            new GenericIdentity("Barney"), new string[] { });
        DeleteDatabase(); // boom
    }

    [PrincipalPermission(SecurityAction.Demand, Role = Roles.Administrator)]
    public static void DeleteDatabase()
    {
        Console.WriteLine(
            Thread.CurrentPrincipal.Identity.Name + " has deleted the database...");
    }
}

但是,这对细粒度访问没有帮助(即“Fred 可以访问客户 A 但不能访问客户 B”)。


附加;当然,对于细粒度,您可以在运行时通过检查主体上的 IsInRole 来简单地检查所需的角色:

static void EnforceRole(string role)
{
    if (string.IsNullOrEmpty(role)) { return; } // assume anon OK
    IPrincipal principal = Thread.CurrentPrincipal;
    if (principal == null || !principal.IsInRole(role))
    {
        throw new SecurityException("Access denied to role: " + role);
    }
}
public static User GetUser(string id)
{
    User user = Repository.GetUser(id);
    EnforceRole(user.AccessRole);
    return user;
}

您还可以编写自己的主体/身份对象来对角色进行惰性测试/缓存,而不必事先了解所有这些:

class CustomPrincipal : IPrincipal, IIdentity
{
    private string cn;
    public CustomPrincipal(string cn)
    {
        if (string.IsNullOrEmpty(cn)) throw new ArgumentNullException("cn");
        this.cn = cn;
    }
    // perhaps not ideal, but serves as an example
    readonly Dictionary<string, bool> roleCache =
        new Dictionary<string, bool>();
    public override string ToString() { return cn; }
    bool IIdentity.IsAuthenticated { get { return true; } }
    string IIdentity.AuthenticationType { get { return "iris scan"; } }
    string IIdentity.Name { get { return cn; } }
    IIdentity IPrincipal.Identity { get { return this; } }

    bool IPrincipal.IsInRole(string role)
    {
        if (string.IsNullOrEmpty(role)) return true; // assume anon OK
        lock (roleCache)
        {
            bool value;
            if (!roleCache.TryGetValue(role, out value)) {
                value = RoleHasAccess(cn, role);
                roleCache.Add(role, value);
            }
            return value;
        }
    }
    private static bool RoleHasAccess(string cn, string role)
    {
        //TODO: talk to your own security store
    }
}

关于c# - .NET 中的身份验证、授权、用户和角色管理以及一般安全性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1222974/

相关文章:

C# 线程多次调用但运行一次

http - 多个 HTTP 授权 header ?

authorization - 我应该使用什么角色以编程方式在 Cloudant/BigCouch/CouchDB 中创建和删除数据库?

asp.net-mvc - ASP.Net MVC 5 身份角色添加到数据库但 User.IsInRole 总是返回 false

c# - 我可以用 C# 编写 Windows 8 RT 游戏吗

c# - 在 C# 中捕获关闭时出错

c# - 如何在不刷新 C# 窗口图像的情况下刷新窗口?

Android 词典安全问题

java - 请帮助-卡住无效 key 异常

javascript - 如何信任在互联网上找到的 javascript 开源库