I'm using VB.NET 2010.
One of my lines of code is:
Encoding.UTF8.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox_AccessCode.Text, "MD5"))
But FormsAu开发者_如何学Cthentication is underlined and the error reads 'FormsAuthentication' is not declared. I've ensured that the System.Web.Security namespace is imported, yet I still receive the message.
Any ideas?
Thank you.
FormsAuthentication forms part of the System.Web that is used in asp.net and is not accessibly through Win Forms. Not entirely sure if you will be able to import the dll and use it that way, I doubt it...
If you just want to hash a md5 string you can do below:
new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] bs = System.Text.Encoding.UTF8.GetBytes(TextBox_AccessCode.Text);
x.ComputeHash(bs);
Thanks to TBohnen.jnr, I found that Forms Authentication is not a part of Windows Forms via VB.NET. I ended up using the following code to generate an MD5 hash:
Public Shared Function MD5(ByVal str As String) As String
Dim provider As MD5CryptoServiceProvider
Dim bytValue() As Byte
Dim bytHash() As Byte
Dim strOutput As String = ""
Dim i As Integer
provider = New MD5CryptoServiceProvider()
bytValue = System.Text.Encoding.UTF8.GetBytes(str)
bytHash = provider.ComputeHash(bytValue)
provider.Clear()
For i = 0 To bytHash.Length - 1
strOutput &= bytHash(i).ToString("x").PadLeft(2, "0")
Next
Return strOutput
End Function
精彩评论