根据此处 .htpasswd散列方法的文档,下面应该生成一个合适的输出行,可以使用SHA1作为散列算法插入到.htpasswd文件中:
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));
}
}
Run Code Online (Sandbox Code Playgroud)
例:
Console.WriteLine(CreateUser("TestUser", "TestPassword"));
Run Code Online (Sandbox Code Playgroud)
将输出:
TestUser:{SHA}YlBiWyJt9ihwriOvjT+sB2DXFYg=
使用此方法构造有效的.htaccess文件应该很简单.