您好我正在創建一個登錄表單從頭開始在C#使用3層。我設法構建了一個工作表單來檢查用戶數據是否正確。如果他填寫了錯誤的數據,他會收到一條消息。但是現在我需要創建一個會話來存儲這個ID。在C#中創建會話#
我搜查了網頁,他們說你必須添加Session["sessionName"]= data
,但是如果我輸入Session["userId"]=s.studentNummer
他不會識別任何東西。將會話放在DAL或DLL中會更好嗎?我想寫在DAL(功能checkLogin)中。有人能幫幫我嗎?
這裏是我的代碼:
DALstudent.cs
public class DALstudent
{
dc_databankDataContext dc = new dc_databankDataContext();
public void insertStudent(Student s)
{
dc.Students.InsertOnSubmit(s);
dc.SubmitChanges();
}
public bool checkLogin(string ID, string passw)
{
bool canlogin = false;
var result = (from s in dc.Students
where s.studentNummer == ID && s.studentPasswoord == passw
select s).Count();
if (result == 1)
{
canlogin = true;
}
else
{
canlogin = false;
}
return canlogin;
}
}
BLLstudent.cs
public class BLLstudent
{
DALstudent DALstudent = new DALstudent();
public void insertStudent(Student s)
{
DALstudent.insertStudent(s);
}
public string getMD5Hash(string passwd)
{
MD5CryptoServiceProvider x = new MD5CryptoServiceProvider();
byte[] bs = Encoding.UTF8.GetBytes(passwd);
bs = x.ComputeHash(bs);
StringBuilder str = new StringBuilder();
foreach (byte b in bs)
{
str.Append(b.ToString("x2").ToLower());
}
string password = str.ToString();
return password;
}
public bool checkLogin(string ID, string passw)
{
bool canlogin = DALstudent.checkLogin(ID, passw);
if (canlogin == true)
{
return true;
}
else
{
throw new Exception("Uw gegevens kloppen niet");
}
}
}
login.aspx.cs
public partial class web_login : System.Web.UI.Page
{
protected void btnLogin_Click(object sender, EventArgs e)
{
try
{
BLLstudent BLLstudent = new BLLstudent();
var loginNr = txtLoginNr.Text;
var pass = BLLstudent.getMD5Hash(txtWachtwoord.Text);
var passw = pass;
BLLstudent.checkLogin(loginNr, passw);
Response.Redirect("student/s_procedure_goedkeuring.aspx");
}
catch (Exception Ex)
{
lblFeedback.Text = Ex.Message;
}
}
}
請詳細說明「他不承認任何事情。」此外,會話狀態不會進入DAL或BLL。它完全屬於Web應用程序。 – David 2011-12-15 17:13:18
是否與用戶用於登錄的用戶ID相同?如果是,那麼你可以直接使用HttpContext.Current.User.Identity.Name.ToString()來訪問它,只要你想要id數據,而不需要在會話中存儲它 – 2011-12-15 17:23:55