【问题标题】:Create .htpasswd files within C#在 C# 中创建 .htpasswd 文件
【发布时间】:2013-02-05 05:19:07
【问题描述】:

对于自动化网站部署的小工具,我想生成 .htaccess 和 .htpasswd 文件。 如何为代码中的密码创建哈希?

【问题讨论】:

  • 能不能不用Apache自带的现有“小工具”(htpasswd)?
  • @Iridium:我试过了,谢谢你的提示。这有点不舒服。我将您的方法与 SHA1 哈希一起使用。

标签: c# .htaccess web .htpasswd


【解决方案1】:

根据关于 .htpasswd 散列方法 here 的文档,以下应生成可插入到 .htpasswd 文件中的合适输出行,使用 SHA1 作为散列算法:

public static string CreateUser(string username, string password)
{
    using (var sha1 = SHA1.Create())
    {
        var hash = sha1.ComputeHash(Encoding.UTF8.GetBytes(password));
        return string.Format("{0}:{{SHA}}{1}", username, Convert.ToBase64String(hash));
    }
}

例子:

Console.WriteLine(CreateUser("TestUser", "TestPassword"));

将输出:

TestUser:{SHA}YlBiWyJt9ihwriOvjT+sB2DXFYg=

使用此方法构造有效的 .htaccess 文件应该很简单。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-05
    • 2010-12-14
    • 2010-09-05
    • 2023-03-23
    相关资源
    最近更新 更多