【问题标题】:Custom membership provider in web ApplicationsWeb 应用程序中的自定义成员资格提供程序
【发布时间】:2013-08-03 02:34:46
【问题描述】:

我需要在我的 Web 应用程序中为 Membership 实现我自己的自定义提供程序。在部分成功实施后,我正在寻找以下问题的答案:

  • 我需要在我的应用程序中覆盖哪些功能
  • 我需要继承的所有类。到目前为止,我使用了类:MemberShipProvider、RoleProvider。
  • ASP.NET 能否自动连接到我的数据库的正确表。有没有这方面的设置?喜欢指定默认​​表名?
  • 是否有任何内置函数可以将密码设置为 Salted Hash?还是我需要自己实现?
  • WAT 工具仍然对我有帮助,或者它的功能会受到限制,因为我发现它可能至少有助于调试?

我发现我错过了一些要覆盖的功能,而且我认为我对 RoleProvider 也做了同样的事情。如果有任何完整的列表,或者这样......那一定会对我有所帮助

【问题讨论】:

    标签: asp.net .net asp.net-membership membership-provider


    【解决方案1】:

    1) 我需要在我的应用程序中覆盖哪些功能

    您主要需要覆盖 GetUser 和 ValidateUser 才能让 Membership Provider 与登录控件一起使用。其余的都是可选的。

    public class CustomMembershipProvider : MembershipProvider
    {   
      public override MembershipUser GetUser(string username, bool userIsOnline)
      {
          throw new NotImplementedException();
      }
    
      public override bool ValidateUser(string username, string password)
      {
          throw new NotImplementedException();
      }
    }  
    

    2) 我需要继承的所有类。到目前为止,我使用了类: MemberShipProvider、RoleProvider。

    MembershipProvider 是必须的。如果要通过角色对用户进行授权,则需要实现 RoleProvider。

    3) ASP.NET 能否自动连接到我的正确表 数据库。有没有这方面的设置?比如指定默认值 表名?

    您覆盖 Membership Provider 的原因是您想要使用您创建的自定义表。您负责从数据库返回数据;成员资格提供者不再需要知道您的表的名称。因此,答案是否定的——没有设置。

    4) 是否有任何内置函数可以将密码设置为 Salted Hash? 或者我需要自己实现这个?

    这里是 Membership Provider 用来生成哈希密码的方法 -

    private static string GenerateSalt()
    {
        byte[] numArray = new byte[16];
        (new RNGCryptoServiceProvider()).GetBytes(numArray);
        string base64String = Convert.ToBase64String(numArray);
        return base64String;
    }
    
    private string EncodePassword(string pass, int passwordFormat, string salt)
    {
        byte[] numArray;
        byte[] numArray1;
        string base64String;
        bool length = passwordFormat != 0;
        if (length)
        {
            byte[] bytes = Encoding.Unicode.GetBytes(pass);
            byte[] numArray2 = Convert.FromBase64String(salt);
            byte[] numArray3 = null;
    
            HashAlgorithm hashAlgorithm = HashAlgorithm.Create(Membership.HashAlgorithmType);
    
            if (hashAlgorithm as KeyedHashAlgorithm == null)
            {
                numArray1 = new byte[(int) numArray2.Length + (int) bytes.Length];
                Buffer.BlockCopy(numArray2, 0, numArray1, 0, (int) numArray2.Length);
                Buffer.BlockCopy(bytes, 0, numArray1, (int) numArray2.Length, (int) bytes.Length);
                numArray3 = hashAlgorithm.ComputeHash(numArray1);
            }
            else
            {
                KeyedHashAlgorithm keyedHashAlgorithm = (KeyedHashAlgorithm) hashAlgorithm;
                if (keyedHashAlgorithm.Key.Length != numArray2.Length)
                {
    
                    if (keyedHashAlgorithm.Key.Length >= (int) numArray2.Length)
                    {
                        numArray = new byte[(int) keyedHashAlgorithm.Key.Length];
                        int num = 0;
                        while (true)
                        {
                            length = num < (int) numArray.Length;
                            if (!length)
                            {
                                break;
                            }
                            int num1 = Math.Min((int) numArray2.Length, (int) numArray.Length - num);
                            Buffer.BlockCopy(numArray2, 0, numArray, num, num1);
                            num = num + num1;
                        }
                        keyedHashAlgorithm.Key = numArray;
                    }
                    else
                    {
                        numArray = new byte[(int) keyedHashAlgorithm.Key.Length];
                        Buffer.BlockCopy(numArray2, 0, numArray, 0, (int) numArray.Length);
                        keyedHashAlgorithm.Key = numArray;
                    }
                }
                else
                {
                    keyedHashAlgorithm.Key = numArray2;
                }
                numArray3 = keyedHashAlgorithm.ComputeHash(bytes);
            }
    
            base64String = Convert.ToBase64String(numArray3);
        }
        else
        {
            base64String = pass;
        }
        return base64String;
    }
    

    5) WAT 工具仍然对我有帮助,否则将受到限制 功能,我发现它可能至少有助于调试?

    是的,您仍然可以使用网站管理工具,但这取决于您覆盖的那些方法。

    例如,如果您不覆盖角色提供者,则无法将用户分配给角色。

    http://www.asp.net/general/videos/how-do-i-create-a-custom-membership-provider http://www.davidhayden.com/blog/dave/archive/2007/10/11/CreateCustomMembershipProviderASPNETWebsiteSecurity.aspx http://www.shiningstar.net/aspnet_articles/customprovider/CustomProvider.aspx http://www.devx.com/asp/Article/29256/0/page/3 http://www.15seconds.com/issue/050216.htm http://www.codeproject.com/KB/aspnet/CustomMembershipProviders.aspx http://www.codeproject.com/KB/aspnet/WSSecurityProvider.aspx

    【讨论】:

    • 感谢您耐心回答我的一长串问题。第 4 点对我来说是全新的。这些链接也很有帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多