【发布时间】:2012-01-21 09:00:56
【问题描述】:
您好,我正在使用 3 层在 c# 中从头开始创建登录表单。我设法构建了一个工作表单来检查用户数据是否正确。如果他填写了错误的数据,他会收到一条消息。但是现在我需要创建一个会话来存储 id。
我在网上搜索过,他们说你必须添加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 应用程序。
-
你的用户ID和用户登录的ID一样吗?如果是,那么您可以在需要 id 数据时使用 HttpContext.Current.User.Identity.Name.ToString() 直接访问它,而无需将其存储在会话中
-
MD5 不是安全哈希。使用 SHA1 或 SHA2,别忘了加盐。