Witam !
Mam taki problem zrobiłem sobie dll-kę która przy uruchomieniu aplikacji ma pobierać numer seryjny z bazy. Do tego celu do aplikacji .net wysyłam sparametryzowany adres url i metodą get response odbieram odpowiedź wygląda to tak:
#ze strony aplikacji:
string UserEmail = UserNameTB.Text;
string UserPassword = PasswordTB.Text;
string UserName = UserEmail;
string Password = UserPassword;
Byte[] password = new Byte[16];
//create the MD5CryptoServiceProvider object we will use to encrypt the password
MD5CryptoServiceProvider md5Hasher = new MD5CryptoServiceProvider();
//create an array of bytes we will use to store the encrypted password
Byte[] hashedBytes;
//Create a UTF8Encoding object we will use to convert our password string to a byte array
UTF8Encoding encoder = new UTF8Encoding();
//encrypt the password and store it in the hashedBytes byte array
hashedBytes = md5Hasher.ComputeHash(encoder.GetBytes(Password));
//StrPassword = null;
foreach (Byte b in hashedBytes)
{
StrPassword += b.ToString() + " ";
}
try
{
Uri uri = new Uri("http://xxxx/xxxx.aspx");
string data = "username=" + UserNameTB.Text + "&hashedbytes=" + StrPassword + "&ID=" + sebBaseString.Text;
textBox1.Text = data;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);
request.Method = WebRequestMethods.Http.Post;
request.ContentLength = data.Length;
request.ContentType = "application/x-www-form-urlencoded";
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(data);
writer.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string tmp = reader.ReadToEnd();
response.Close();
label5.Text = tmp;
sebPassword.Text = tmp;
}
catch (Exception ex)
{
label5.Text = ex.ToString();
}
#A po stronie aplikacji webowej wygląda to tak:
string username = Request["username"];
string hashedBytes = Request["hashedbytes"];
string ID = Request["ID"];
Byte[] password = new Byte[16];
sqlhelper.AddParameterToSQLCommand("UserEmail", SqlDbType.VarChar);
sqlhelper.SetSQLCommandParameterValue("UserEmail", username);
SqlDataReader rdr = sqlhelper.GetReaderByCmd("GetHash");
sqlhelper.RemoveParameterFromSQLCommand("UserEmail");
if (rdr.Read())
{
password = (Byte[])rdr["UserPassword"];
}
foreach (Byte b in password)
{
StrDBPassword += b.ToString() + " ";
}
StrPassword = hashedBytes;
if (StrPassword == StrDBPassword)//jesli haslo OK
{
...
Response.Clear();
Response.Write(rdr2[0].ToString());//w celach testowych
}
else // jesli haslo zle
{
Response.Clear();
Response.Write("Z bazy:" + StrDBPassword + " zakodowane:" + StrPassword + " 0-Nieprawdłowa nazwa użytkownika i/lub hasło");
}
I tak jeśli wyślę to po raz pierwszy lub odkomentuje //StrPassword = null; to serwer zwraca mi błąd 500
Jeśli nie jest odkomentowane to za drugim razem dostaje od serwera odpowiedź, że hasło nieprawidłowe co się oczywiście zgadza bo hasło nie jest zerowane.
#Pytanie tylko dlaczego serwer reaguje za drugim razem dopiero ?