PDA

View Full Version : تفکیک مقادیر مختلف هش شده درون یک کوکی



faramarz_s
سه شنبه 15 مهر 1382, 17:03 عصر
من دو مقدار یوزر نیم و پسورد را درون 2 کوکی مجزا ذخیره میکنم آنگاه برای بازیابی آنها با مشکل خوانده نشدن دومین کوکی مواجه می شوم.


private void Page_Load(object sender, System.EventArgs e)
{
String strCookieUsername = TextBox1.Text;
HttpCookie CookieUsername1 = Request.Cookies[strCookieUsername];

String strCookiePass = TextBox2.Text;
HttpCookie Cookiepass1 = Request.Cookies[strCookiePass];

if ((null == CookieUsername1)||(null == Cookiepass1 ) )
{
Response.Write("Cookies for logging on not found. <br><hr>");
}
else
{
String strCookieValueU = CookieUsername1.Value.ToString();
String strCookieValueP = Cookiepass1.Value.ToString();
//Response.Write("<br><br><br><br><br><br>The " + strCookieUserID + " CookieUsername1 contains:<b>"+ strCookieValue + "<b>");
Response.Write("strCookieValueUsername = "+strCookieValueU+"");
Response.Write("<br><hr>strCookieValuePassword = "+strCookieValueP+"");

if (isUserValid(strCookieValueU,strCookieValueP))
//Response.Redirect("Wellcome.aspx");
Response.Write("<br><br><br><br><br>"+"Successful Login with cookie");
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

bool isUserValid(string userID, string userPass)
{
//1. Create a connection
string strConnString = "server=localhost;uid=sa;pwd=yaali;database=Persia";
SqlConnection objConn = new SqlConnection(strConnString);

//2. Create a command object for the query
string strSQL ="SELECT COUNT(*) FROM tblAccounts " +
"WHERE Username=@Username AND Password=@Password";
SqlCommand objCmd = new SqlCommand(strSQL, objConn);

//3. Create parameters

SqlParameter paramUsername = new SqlParameter("@Username",
SqlDbType.VarChar, 50);
paramUsername.Value = userID;
objCmd.Parameters.Add(paramUsername);

//Encrypt the password
byte[] hashedDataBytes = new Byte[16];
clsCrypt cr = new clsCrypt() ;
hashedDataBytes = cr.computeMD5Hash (userPass);

SqlParameter paramPwd = new SqlParameter("@Password",
SqlDbType.Binary, 16);
paramPwd.Value = hashedDataBytes;
objCmd.Parameters.Add(paramPwd);

//Insert the records into the database
objConn.Open();
int iResults = (int) objCmd.ExecuteScalar();
objConn.Close();

if( iResults == 1 )
//The user was found in the DB
return true;
else
//The user was not found in the DB
return false;
}

private void Button1_Click(object sender, System.EventArgs e)
{
//CookiePassword
HttpCookie CookiePassword = new HttpCookie(TextBox2.Text);
CookiePassword.Value =FormsAuthentication.HashPasswordForStoringInConfi gFile(TextBox2.Text,"SHA1");
DateTime dtNow2 = DateTime.Now;
TimeSpan tsMinute2 = new TimeSpan(0, 0, 1, 0);
CookiePassword.Expires = dtNow2 + tsMinute2;
Response.Cookies.Add(CookiePassword);
Response.Write("<br><br>Password hashed by SH1 ALGORITHM and Cookie wrote it.<br><hr>");

//------------------------------------------------------------------------------------------------------ //CookieUsername
HttpCookie CookieUsername = new HttpCookie(TextBox1.Text);
CookieUsername.Value =TextBox1.Text;
DateTime dtNow1 = DateTime.Now;
TimeSpan tsMinute1 = new TimeSpan(0, 0, 1, 0);
CookieUsername.Expires = dtNow1 + tsMinute1;
Response.Cookies.Add(CookieUsername);
Response.Write("<br><br>CookieUsername wrote.<br><hr>");

//------------------------------------------------------------------------------------------------------
if (isUserValid(TextBox1.Text.ToString(),TextBox2.Tex t.ToString()))
//Response.Redirect("Wellcome.aspx");
Response.Write("<br><br><br><br><br>"+"Successful Login WithOut cookie!");
}
اگر لازمست درون یک کوکی این دو مقدار را بخوانم سوال آنست که چگوه مقادیر هش شده را باید تفکیک کنم؟

Vahid_Nasiri
سه شنبه 15 مهر 1382, 23:05 عصر
لزومی ندارد از دو کوکی استفاده کنید . می توانید از یک کوکی با ساب کی های مختلف استفاده نمایید.
برای مثال

محتویات کوکی ( بد نیست آنرا در قسمت temp اینترنت اکسپلورر مشاهده کنید ) :

info
up=has1
.
.
.
info2
up2=hash2
.
.
.

و الی آخر (زمان اکسپایر شدن هم فراموش نشود)

faramarz_s
چهارشنبه 16 مهر 1382, 16:09 عصر
دقیقا راه را متوجه شدم . تشکر فراوان :o
یک نمونه برنامه در مورد ساب کی ها را هم آدرسش را لطف کنید :wink:

Vahid_Nasiri
چهارشنبه 16 مهر 1382, 20:16 عصر
اگر MSDN را داری یک سرچ کوچولو کلی مطلب در مورد کوکی به شما می دهد :wink: