AspNet成员身份加密密码生成

时间:2019-07-18 14:54:53

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

在我们开始之前,我想澄清一下,我知道这个问题有多严重。这是一个可怕的情况,但是我受到一些非常奇怪的规范的束缚。

我知道,您不仅不应该尝试为but Microsoft have made it clear this shouldn't even be used这种东西编写自己的包装器。因此,在您开始写“为什么还要这样做”的答复之前,请尝试理解我已经与上级进行了这种交谈,但是对新功能的追求和时间的缺乏意味着尽管它很残酷;尽管如此-我在这里。

我们有一个ASP Net成员数据库,该数据库开始于我在这家公司之前的某个时间,现在已经有大约4万名用户。我们在.Net 3.5中有一个平台,可以让用户登录。

我目前的工作是在.Net Core 2.1中编写一个API,该API的一部分允许用户创建和更新,但是存在一个问题-从成员身份迁移到身份不是一种选择,因此有人告诉我在Membership数据库中为存储过程创建一个包装器。

这主要是成功的,唯一的问题是;这个问题的主题。通过aspnet_Membership_CreateUser创建用户,我需要以可以在我们的平台中成功验证数据的方式提交数据。

我最初遵循this post,但发现它是为PasswordFormat 1-Hashed设计的;然后,我发现我们的用户群使用了PasswordFormat 2-Encrypted,因此我正在创建的用户无法验证。

代码看起来像这样

    public bool CreateUser(string userName, string password, string email, string securityQuestion, string securityAnswer, bool isApproved)
    {
        bool success = false;

        //Just so I can attempt to login afterwards
        password = "Hello World!";

        //Our password and password salt need to be base64 encoded before we can save them to the DB
        string salt = Guid.NewGuid().ToString();
        string encryptedSalt = salt.Base64Encode();

        //Concatenate our salt and password
        IEnumerable<byte> saltedpass = salt.GetBytes(Encoding.UTF8).Concat(password.GetBytes(Encoding.UTF8));

        //Use SHA1 to hash more - equivilant to the HASHBYTES('SHA1' T-SQL
        byte[] sha1HashedPass = PasswordHelper.HashBytes(_validationMethod, saltedpass.ToArray(), _validationKey);
        string hashedPass = sha1HashedPass.ToBase64String();

        int errorCode = MembershipCreateUser(_applicationName, userName, hashedPass, encryptedSalt, email, securityQuestion, securityAnswer, isApproved);
        if (errorCode == 0)
        {
            success = true;
        }
        return success;
    }

值得一提的是,_validationKey是使用此数据库的应用程序之间共享的机器密钥,我将其传递给SHA1机制。

因此,撇开故意和可悲的不良安全做法,可以忽略不计; C#中是否可以通过这种方式生成加密的(而不是散列的)密码和盐?

1 个答案:

答案 0 :(得分:0)

感谢您的评论-幸好我们能够在我们的平台中支持Hashed密码;问题出在我的代码上,而不是ASP成员身份。

如前所述,我正在撰写一篇originally written in T-SQL的文章,并尝试构建其C#实现。我的代码实现不正确,因此生成的密码和盐无法通过ASP Net Membership验证,这在我的原始帖子中并不明显,因为我混淆了SHA1哈希数据的方法。

    //Using hard coded just for example
    string username = "joeborder";
    string password = "Hello World!";
    string salt = "TastySalt";
    Encoding encoder = Encoding.Unicode;    //Encoding was also incorrect

    //Our password and password salt need to be base64 encoded before we can save them to the DB
    string encryptedSalt = salt.Base64Encode();

    //Concatenate our salt and password
    IEnumerable<byte> saltedpass = salt.GetBytes(encoder).Concat(password.GetBytes(encoder));

    //Use SHA1 to hash more - equivilant to the HASHBYTES('SHA1') T-SQL
    var SHA1Hasher = new SHA1CryptoServiceProvider();    //Originally I was using HMACSHA1 which was producing a different output
    byte[] sha1HashedPass = SHA1Hasher.ComputerHash(saltedpass.ToArray());
    string hashedPass = sha1HashedPass.ToBase64String();

    /*
        EXEC aspnet_Membership_CreateUser
            @ApplicationName = "MyApp",
            @UserName = username,
            @Password = hashedPass,
            @PasswordSalt = encryptedSalt,
            ...Etc
    */

然后在我们的.Net 3.5应用程序中,以下代码将起作用

    string username = "joeborder";
    string password = "Hello World!";

    if (Membership.ValidateUser(username, password))
    {
        Console.WriteLine("You've gotta be kidding me thats a clutch");
    }