c# - C# 中的 Dictionary.ContainsKey() 未在字典中找到键

标签 c# authentication dictionary permissions user-permissions

<分区>

我正在尝试为特权/权限编写一个类。

我正在尝试使用 C# 中的二进制基本权限系统。

我在我认为它应该起作用的地方得到了它。但是,它没有按预期工作。

之后,我逐步检查了我的代码,我可以在名为 _mapPermissions 的方法中看到一个问题,即 if (dic.ContainsKey(vv)) 行每次都返回 false。

键是具有 2 个值(即 secionNameKeyIndex)的类的实例

这是我的整个类(class)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using MySql.Data.MySqlClient;

namespace POS
{
    class Roles
    {

        private Dictionary<PermissionsKey, int> systemPermissions = new Dictionary<PermissionsKey, int>();
        private Dictionary<PermissionsKey, int> userPermissions = new Dictionary<PermissionsKey, int>();

        public Roles()
        {
            this._getSystemPrivleges();
            this._getUserPrivleges();

            this._display(systemPermissions);
            this._display(userPermissions);

        }

        public bool hasAccess(string sectionName, string[] keys)
        {

            if (    this.systemPermissions.Count == 0
                 || this.userPermissions.Count == 0
                 || keys.Count() == 0
                 || String.IsNullOrEmpty(sectionName) 
             )
            {
                return false;
            }

            int systemReq = this._mapPermissions(sectionName, keys, systemPermissions); ;
            int userPerm = this._mapPermissions(sectionName, keys, userPermissions);



            int check =  systemReq & userPerm;

            Common.Alert("System Value" + systemReq + Environment.NewLine +"User Value " + userPerm + Environment.NewLine + "AND Results " + check);


            if (check == 0)
            {
                return false;
            }

            return true;

        }

        private int _mapPermissions(string secName, string[] keys, Dictionary<PermissionsKey, int> dic)
        {

            int newKey = 0;
            var vv = new PermissionsKey();

            foreach (string k in keys)
            {
                vv.sectionName = secName;
                vv.KeyIndex = k;

                if (dic.ContainsKey(vv))
                {
                    newKey |= dic[vv] ;
                }
            }

            return newKey;
        }



        private void _getSystemPrivleges()
        {
            var db = new dbConnetion();

            string sql =   " SELECT SQL_CACHE "
                         + " KeyIndex, Value, sectionName "
                         + " FROM role_keys "
                         + " WHERE status = 'active' ";

            foreach (var i in db.getData(sql, null, r => new SystemPermissions()
                                                           {
                                                                 pIndex = r["KeyIndex"].ToString()
                                                               , pSec = r["sectionName"].ToString()
                                                               , pValue = r["Value"].ToString()
                                                           }
                                           )
                    )
            {

                var vv = new PermissionsKey();
                vv.sectionName = i.pSec;
                vv.KeyIndex = i.pIndex;
                systemPermissions.Add(vv, Convert.ToInt32(i.pValue));

            }
        }

        private void _getUserPrivleges()
        {
            var db = new dbConnetion();

            string sql = " SELECT SQL_CACHE k.sectionName, k.KeyIndex, k.value "
                        +" FROM users AS su "
                        +" INNER JOIN role_relation AS r ON r.roleID = su.roleID "
                        +" INNER JOIN role_keys AS k ON k.KeyID = r.KeyID "
                        +" WHERE su.status = 'active' AND su.userID = @userid ";

            var parms = new List<MySqlParameter>();
            parms.Add(new MySqlParameter("@userid", UserInfo.UserID));

            foreach (var i in db.getData(sql, parms , r => new SystemPermissions()
                                                            {
                                                                  pIndex = r["KeyIndex"].ToString()
                                                                , pSec = r["sectionName"].ToString()
                                                                , pValue = r["Value"].ToString()
                                                            }
                                           )
                    )
            {
                var vv = new PermissionsKey();
                vv.sectionName = i.pSec;
                vv.KeyIndex = i.pIndex;

                userPermissions.Add(vv, Convert.ToInt32(i.pValue));
            }
        }



        private void _display(Dictionary<PermissionsKey, int> dic)
        {
            string str = "";
            foreach (KeyValuePair<PermissionsKey, int> d in dic)
            {
                var vv = new PermissionsKey();
                var c = d.Key;

                str += c.sectionName + "_" + c.KeyIndex + "  =>  " + d.Value.ToString() + Environment.NewLine;

            }

            Common.Alert(str);
        }



        private int _BinToInt(string str)
        {
            Regex binary = new Regex("^[01]{1,32}$", RegexOptions.Compiled);
            int val = -1;

            if (binary.IsMatch(str))
            {
                val = Convert.ToInt32(str, 2);
            }

            return val;
        }

        private string _IntToBin(int number)
        {
            return Convert.ToString(number, 2);
        }


    }

    class SystemPermissions{
        public string pIndex;
        public string pSec;
        public string pValue;
    }

    class PermissionsKey
    {
        public string sectionName;
        public string KeyIndex;
    }
}

我是这样使用这个类的

    var role = new Roles();
    string[] aa = { "view", "use" };

    if (role.hasAccess("system", aa))
    {
        Common.Alert("Welcome");
    }
    else
    {
        Common.Alert("NOP!");
    }

请注意方法 Common.Alert() 它只是显示一条消息(即 MessageBox)

以上代码运行时显示如下

1)systemPermissions字典的内容按以下顺序PermissionsKey.sectionName_PermissionsKey.KeyIndex => 值 (即 1、2、4、8、16、32、64 .....) 系统_do = > 1 系统使用 => 2 系统 View => 4 测试使用 => 1 测试 View => 2

2)usePermissions字典的内容

system_do = > 1
test_use => 1

问题是 2 个变量 systemRequserPerm 每次都返回 0。我不知道为什么

 int systemReq = this._mapPermissions(sectionName, keys, systemPermissions); ;
 int userPerm = this._mapPermissions(sectionName, keys, userPermissions);

最佳答案

为了将某物用作字典键并期望它符合您对什么被认为是相等的期望,您实际上需要定义什么被认为是相同的。

在你的PermissionsKey类,你不要覆盖 Equals ,这意味着默认的 Equals 方法仍在发挥作用(这是对象引用,并且对于每个特定对象都是唯一的)。

要修复,您需要实际告诉字典如何评估相等性。任何时候你覆盖 Equals , 你也应该 also override GetHashCode :

class PermissionsKey
{
    private string sectionName;
    private string keyIndex;

    public string SectionName { get { return sectionName; } }
    public string KeyIndex { get { return keyIndex; } }

    public PermissionsKey(string sectionName, string keyIndex)
    {
        this.sectionName = sectionName;
        this.keyIndex = keyIndex;
    }

    public override bool Equals(object obj)
    {
        var key = obj as PermissionsKey;

        if (key == null)
            return false;

        return sectionName.Equals(key.sectionName) &&
               keyIndex.Equals(key.keyIndex);
    }


    //Credit to Jon Skeet from https://stackoverflow.com/questions/263400/what-is-the-best-algorithm-for-an-overridden-system-object-gethashcode
    //  for inspiration for this method
    public override int GetHashCode()
    {
        unchecked // Overflow is fine, just wrap
        {
            int hash = (int) 2166136261;
            // Suitable nullity checks etc, of course :)
            hash = hash * 16777619 ^ sectionName.GetHashCode();
            hash = hash * 16777619 ^ keyIndex.GetHashCode();
            return hash;
        }
    }
}

您正在覆盖 Equals 以实际定义相等性比较。任何时候你试图比较 2 个对象(特别是在 Dictionary 对象中,但任何其他时候你通过调用 .Equals 来比较),你应该定义你自己的比较。如果你不这样做,那么你只是在使用默认的相等比较,这是一个纯粹的反对比较,除非从同一个对象创建,否则 2 个对象永远不会相等。

同样,当您覆盖 Equals 时,强烈建议您也覆盖 GetHashCode . HashCode 定义对象属于哪个“桶”。遵循 Equals 实现的良好哈希码将通过仅将具有相同哈希码的对象放入相同的桶中来帮助加快比较。标准建议是 2 个相等的对象将始终具有相同的哈希码,但 2 个不等于的对象也可能具有相同的哈希码。实际上,这是框架检查不平等的一种快速方法。如果您获得 2 个不同的哈希码,框架知道对象不相等,但如果哈希码相同,那么它会检查是否相等。

如果您不能或不想覆盖 Equals 和 GetHashCode,那么您还可以选择定义 IEqualityComparer<T> 在字典的构造函数中,

你只需要传递一个实现了IEqualityComparer<T>的对象在该对象中,您可以定义所需的相等比较。

此外,任何你打算用作字典键的东西都应该永远是可变的,特别是用于计算相等性和计算哈希码的字段,否则你的键可能会因为哈希而丢失对象的代码与最初放入字典的内容不匹配。我已经修改了您的原始示例,将 PermissionsKey 中的字段更改为不可变属性,并添加了一个构造函数以允许您在最初创建时设置它们一次。

关于c# - C# 中的 Dictionary.ContainsKey() 未在字典中找到键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27750832/

相关文章:

c# - 'CompanyName.Foo' 是一个 'namespace' 但像 'type' 一样使用

c# - 使用 C# 解压 tar 文件

php - 在 PHP/MySQL 注销表单中围绕 session 变量包装函数

php - Symfony 2.6 安全 BCryptPasswordEncoder 错误

c# - OWIN 自定义身份验证服务

Python 访问字典列表中的值

C# Task.Wait() 聚合异常

c# - C# 中的 Pinvoke C++ 函数传递 2 个 float

java - Chronicle Map 可以在 Java 1.6 上运行吗?或者低于那个?

列表字典中的 Python 最小值