這是一種使用簡單的本地「web服務」調用來包裝服務器端JScript函數的方法。
添加JScript或ASP腳本引用散列函數:
文件:DoHash.asp
<!-- #include file="passwordHasher.js" -->
<%
Dim password
password = Request.Form("password")
Response.Write HashPassword(password)
%>
在ASP.NET應用程序中,如果有必要的地方相關添加此方法(和類:
public class HashWrapper
{
public static string HashPasswordFromJScript(string password)
{
string url = "https://mysite.com/DoHash.asp";
string fields = "password=" + password;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
using(Stream rs = request.GetRequestStream())
{
using (MemoryStream ms = new MemoryStream())
{
using(BinaryWriter bw = new BinaryWriter(ms))
{
bw.Write(Encoding.UTF8.GetBytes(fields));
ms.WriteTo(rs);
ms.Flush();
}
}
}
using(HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if(response.StatusCode == HttpStatusCode.OK)
{
using(Stream rs = response.GetResponseStream())
{
using (StreamReader rr = new StreamReader(rs))
{
StringBuilder sb = new StringBuilder();
int bufferSize = 1024;
char[] read = new Char[bufferSize];
int count = rr.Read(read, 0, bufferSize);
while (count > 0)
{
sb.Append(read, 0, count);
count = rr.Read(read, 0, bufferSize);
}
return sb.ToString();
}
}
}
return null;
}
}
}
在您需要哈希密碼的應用程序的部分只要致電:
string hashedPassword = HashWrapper.HashPasswordFromJScript(password);
這不是很漂亮,但它會讓你過。我也假定您的網站可以使用SSL加密流量。
來源
2011-02-02 00:11:21
Kev
我想你可以做一些花哨JScript.NET(http://en.wikipedia.org/wiki/JScript_.NET),但更好的解決方案可能是隻是它隱蔽到C#。語法足夠接近(尤其是使用C#2.0+),它應該非常簡單。如果它真的很複雜,你甚至可以試試JLCA:http://www.microsoft.com/downloads/en/details.aspx?FamilyID=46bea47e-d47f-4349-9b4f-904b0a973174&displaylang=en – RMD 2011-02-01 16:29:13