System.Web.Security的替代方案,使用相同的数据库

时间:2011-10-13 20:53:33

标签: c# asp.net transactions membership

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

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

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

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

非常感谢任何人提出任何意见!

1 个答案:

答案 0 :(得分: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);
}
相关问题