C# 用C语言创建会话#
大家好,我正在用c#从头开始创建一个登录表单,使用3层。我已经成功构建了一个工作表单来检查用户数据是否正确。如果他填错了数据,他会收到一条信息。但是现在我需要创建一个会话来存储id 我在网上搜索过,他们说你必须添加C# 用C语言创建会话#,c#,asp.net,.net,session,C#,Asp.net,.net,Session,大家好,我正在用c#从头开始创建一个登录表单,使用3层。我已经成功构建了一个工作表单来检查用户数据是否正确。如果他填错了数据,他会收到一条信息。但是现在我需要创建一个会话来存储id 我在网上搜索过,他们说你必须添加Session[“sessionName”]=data,但如果我键入Session[“userId”]=s.studentnumer,他什么都认不出来。将会话放在DAL或DLL中更好吗?我想把它写在DAL(函数checkLogin)中。谁能帮帮我吗 这是我的密码: DALstudent
Session[“sessionName”]=data
,但如果我键入Session[“userId”]=s.studentnumer
,他什么都认不出来。将会话放在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;
}
}
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");
}
}
}
BLLstudent.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;
}
}
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;
}
}
}
只能在web应用程序中访问会话,因此您需要从会话中设置和获取值,并将这些值从web传递到其他层。.NET会话状态在表示层中处理,尽管它可以在web worker进程中运行的任何业务逻辑中访问(请注意,还有进程外会话状态,但也可以从表示层进行管理)。在表示层之外与会话交互很少是一种好的做法 在业务层中,可以通过以下方式访问会话:
System.Web.HttpContext.Current.Session
在大多数web实体(页面、控件、视图)中,它仅由会话引用
会话是一个基于键的集合;您将一个值与一个键一起放入,然后使用一个键检索相同的值
protected override void OnLoad( EventArgs e )
{
Session["foo"] = "bar";
string valueFromSession = Session["foo"].ToString();
}
您还可以在会话中使用cookies:
if (SessionHash != null && (!HttpContext.Current.Request.Cookies.AllKeys.Contains("hash")) {
var cookie = new HttpCookie("hash", Convert.ToBase64String(SessionHash)) {
HttpOnly = true
};
HttpContext.Current.Response.Cookies.Set(cookie);
}
// remove cookie on log out.
HttpContext.Current.Request.Cookies.Remove("hash");
请详细说明“他什么都不认识”。此外,会话状态不在DAL或BLL中。它完全属于web应用程序。您的用户id是否与用户用于登录的id相同?如果是,则可以使用HttpContext.Current.user.Identity.Name.ToString()直接访问它无论何时你想要id数据,不需要将其存储在sessionMD5中都不是一个安全的散列。使用SHA1或SHA2,别忘了添加盐。谢谢!!!我已经找到了(它可以工作)。我想我必须把它放在DAL中,在那里查询,就像我在php中做的那样。