Ruby on Rails设计从C#.NET读取的加密密码

时间:2015-02-14 22:12:33

标签: c# asp.net ruby devise password-encryption

我们有一个Ruby on Rails应用程序,它使用 Devise gem进行用户处理,包括创建和验证用户密码。密码在MySQL数据库中显然是加密的。我们使用Devise默认值来加密密码。现在我们有另一个小的同一局域网应用程序(一个C#ASP.NET应用程序)需要使用数据库直接使用用户/密码进行身份验证,以执行一些只读操作。

我们怎样才能最好地模仿Devise在C#ASP.NET应用程序中针对MySQL数据库中的相同数据进行用户/密码身份验证的操作?

基本上,我需要弄清楚如何重新创建Devise的valid_password? C#.NET中的方法

http://www.rubydoc.info/github/plataformatec/devise/Devise/Models/DatabaseAuthenticatable#valid_password%3F-instance_method

# File 'lib/devise/models/database_authenticatable.rb', line 46

def valid_password?(password)
  return false if encrypted_password.blank?
  bcrypt   = ::BCrypt::Password.new(encrypted_password)
  password = ::BCrypt::Engine.hash_secret("#{password}#{self.class.pepper}", bcrypt.salt)
  Devise.secure_compare(password, encrypted_password)
end

3 个答案:

答案 0 :(得分:0)

我想我正确理解了这个问题。您要做的是使用rails应用程序上的凭据对.NET应用程序上的用户进行身份验证。如果是这样,我建议在ruby端实现oauth服务器,在.NET代码中实现客户端。至于服务器,您有几个提供此功能的宝石,包括

  1. oauth-ruby
  2. doorkeeper
  3. oauth2-provider

答案 1 :(得分:0)

Devise使用BCrypt进行密码存储,因此值得庆幸的是,验证过程非常简单。安装BCrypt.Net NuGet软件包(或任何其他BCrypt实现),然后执行以下操作:

public static bool ValidPassword(string userSuppliedPassword, string hashedAndSaltedPassFromDatabase)
{
    return BCrypt.Net.BCrypt.Verify(userSuppliedPassword, hashedAndSaltedPassFromDatabase);
}

答案 2 :(得分:0)

我有一个类似的要求,即从 C# 应用程序验证现有用户。

我为 BCrypt 使用了包 bcrypt.net - next

这是我使用的代码片段

static void Main(string[] args)
    {
        const string DEVISE_PEPPER = "Here comes the devise pepper value";

        // One pass
        string pass = "reE8TuLcZ44XwRz";
        const string passHash = "$2a$10$1Rm.BC0hnF1laC7MFJ/B0eFu2rtG1Asy6fRqJVdqcfBO6LASn4Nqa";

        // Second pass
        const string secondPass = "FpRfaaqzNNRAum9";
        const string secondPassHash = "$2a$10$/Ex4x9LkvxIncaXhByqVP.YRdwRlZFJ7p4H96BfqHk1oGmw3YwLMC";

        // How Ruby BCyrpt verify? Ref - https://www.freecodecamp.org/news/how-does-devise-keep-your-passwords-safe-d367f6e816eb/
        // 1. Fetch the input password
        // 2. Fetch the salt of the stored password
        // 3. Generate the hash from the password and salt using the same bcrypt version and cost factor 
        // 4. Check if the stored hash is the same one as the computed on step 3

        // Ref - https://github.com/heartcombo/devise/blob/5d5636f03ac19e8188d99c044d4b5e90124313af/lib/devise/encryptor.rb#L14
        var finalPass = $"{secondPass}{DEVISE_PEPPER}";

        var verified = BCrypt.Net.BCrypt.Verify(finalPass, secondPassHash);
        var verifiedTxt = verified ? "verified!" : "didn't verified.";
        
        Console.WriteLine($"Password and hash {verifiedTxt}");

        Console.ReadKey();
    }

我成功地verify获得了密码。