【问题标题】:Alternative to System.Web.Security, using same databaseSystem.Web.Security 的替代方案,使用相同的数据库
【发布时间】:2011-10-15 01:13:35
【问题描述】:

我正在编写一个 WinForms 应用程序来迁移大量数据。新系统基于 Web 并使用 ASP.NET 会员 API。

我必须使用事务将大量数据库插入和更新包装到单个事务中。这包括更新用户和角色(aspnet_Users、aspnet_Roles 等)。我已经成功引用了 System.Web.Membership 并在我的应用中使用它来在开始迁移之前验证数据,所以这不是问题。

但是,问题在于迁移期间,当我将所有数据库调用包装在单个事务中时。由于成员资格代码关闭了连接,我收到 DTC 错误,说分布式事务未启用。我想避免更改客户端计算机上的任何内容,因此我正在寻找一种方法来更新具有回滚功能的用户和角色。

目前,据我所知,我唯一的选择是直接调用存储过程,避免使用 Membership API。如果可能的话,我也想避免这种情况,所以我想知道是否有办法在事务中使用 Membership API,或者是否有一个替代库使用相同的数据库表,但可以很好地处理事务。

非常感谢任何人的任何意见!

【问题讨论】:

    标签: c# asp.net transactions membership


    【解决方案1】:

    我最终直接调用了存储过程。我遇到的唯一障碍是创建新用户。它需要密码和安全答案加密,因此我只是从框架源代码中复制了代码。确切地说,来自 System.Web.Security.SqlMembershipProvider.cs

    由于我只使用 SHA1 加密,因此我已删除并修剪了其中的一些代码以适应我自己的情况。

    我很确定我不是唯一遇到此问题的人,因此对于遇到相同问题的其他人,这里有一个用于插入用户的完整代码。其他存储过程更容易调用。

    这里几乎没有错误检查,所以添加你自己的并使用System.Security.Cryptography

    //Call to create a new user and return the ID
    public static Guid? CreateUser(MyDataContext DB, string UserName, string Password, string Email, string PasswordQuestion, string PasswordAnswer)
    {
        string salt = GenerateSalt();
        string password = EncodePassword(Password, salt);
        string encodedPasswordAnswer = EncodePassword(PasswordAnswer.ToLower(), salt);
        DateTime dt = DateTime.UtcNow;
    
        Guid? newUserID = null;
    
        //res would contain the success or fail code from the stored procedure
        //0 = success; 1 = fail;
        int res = DB.aspnet_Membership_CreateUser(  "[My app name]", UserName, password, salt, Email, PasswordQuestion, encodedPasswordAnswer, true, dt, DateTime.Now, 0, 1, ref newUserID);
    
        return newUserID;
    }
    
    private static string GenerateSalt()
    {
        byte[] buf = new byte[16];
        (new RNGCryptoServiceProvider()).GetBytes(buf);
        return Convert.ToBase64String(buf);
    }
    private static string EncodePassword(string pass, string salt)
    {
        byte[] bIn = Encoding.Unicode.GetBytes(pass);
        byte[] bSalt = Convert.FromBase64String(salt);
        byte[] bRet = null;
    
        HashAlgorithm hm = HashAlgorithm.Create("SHA1");
        if (hm is KeyedHashAlgorithm)
        {
            KeyedHashAlgorithm kha = (KeyedHashAlgorithm)hm;
            if (kha.Key.Length == bSalt.Length)
            {
                kha.Key = bSalt;
            }
            else if (kha.Key.Length < bSalt.Length)
            {
                byte[] bKey = new byte[kha.Key.Length];
                Buffer.BlockCopy(bSalt, 0, bKey, 0, bKey.Length);
                kha.Key = bKey;
            }
            else
            {
                byte[] bKey = new byte[kha.Key.Length];
                for (int iter = 0; iter < bKey.Length; )
                {
                    int len = Math.Min(bSalt.Length, bKey.Length - iter);
                    Buffer.BlockCopy(bSalt, 0, bKey, iter, len);
                    iter += len;
                }
                kha.Key = bKey;
            }
            bRet = kha.ComputeHash(bIn);
        }
        else
        {
            byte[] bAll = new byte[bSalt.Length + bIn.Length];
            Buffer.BlockCopy(bSalt, 0, bAll, 0, bSalt.Length);
            Buffer.BlockCopy(bIn, 0, bAll, bSalt.Length, bIn.Length);
            bRet = hm.ComputeHash(bAll);
        }
        return Convert.ToBase64String(bRet);
    }
    

    【讨论】:

      猜你喜欢
      • 2018-01-30
      • 1970-01-01
      • 1970-01-01
      • 2010-10-23
      • 2016-10-22
      • 2013-04-09
      • 2012-12-05
      • 2010-09-10
      • 1970-01-01
      相关资源
      最近更新 更多