在C#创建会话 [英] Create session in C#

查看:154
本文介绍了在C#创建会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是在用3层C#从头开始创建一个登录表单。我已经成功建立,如果用户的数据是正确的检查工作形式。如果他填写了错误的数据,他会得到一个消息。但现在我需要创建一个会话来存储ID。

我在网上搜索,他们说你必须添加会话[SESSIONNAME] =数据,但如果我键入会话[ 用户id] = s.studentNummer 他不承认任何事情。它是更好地把会议在DAL或DLL?我希望把它写在DAL(功能checkLogin)。有人可以帮帮我吗?

下面是我的code:

DALstudent.cs

 公共类DALstudent
{
    dc_databankDataContext DC =新dc_databankDataContext();

    公共无效insertStudent(学生S)
    {
        dc.Students.InsertOnSubmit(多个);
        dc.SubmitChanges();
    }

    公共BOOL checkLogin(串号,串PASSW)
    {
        布尔canlogin = FALSE;
        VAR的结果=(从S在dc.Students
                      其中,s.studentNummer == ID和放大器;&安培; s.studentPasswoord == PASSW
                      选择S).Count之间的();
        如果(结果== 1)
        {
            canlogin = TRUE;
        }
        其他
        {
            canlogin = FALSE;
        }
        返回canlogin;
    }
}
 

BLLstudent.cs

 公共类BLLstudent
{
    DALstudent DALstudent =新DALstudent();

    公共无效insertStudent(学生S)
    {
        DALstudent.insertStudent(多个);
    }

    公共字符串getMD5Hash(字符串的passwd)
    {
        MD5CryptoServiceProvider X =新MD5CryptoServiceProvider();
        byte []的BS = Encoding.UTF8.GetBytes(passwd文件);
        BS = x.ComputeHash(BS);
        StringBuilder的海峡=新的StringBuilder();
        的foreach(在BS字节二)
        {
            str.Append(b.ToString(×2)的tolower());
        }
        串密码= str.ToString();
        返回密码;
    }

    公共BOOL checkLogin(串号,串PASSW)
    {
        布尔canlogin = DALstudent.checkLogin(ID,PASSW);
        如果(canlogin ==真)
        {
            返回true;
        }
        其他
        {
            抛出新的异常(的Uw gegevens kloppen Niet的);
        }
    }
}
 

login.aspx.cs

 公共部分类web_login:System.Web.UI.Page
{
    保护无效btnLogin_Click(对象发件人,EventArgs的)
    {
        尝试
        {
            BLLstudent BLLstudent =新BLLstudent();
            VAR loginNr = txtLoginNr.Text;
            VAR通= BLLstudent.getMD5Hash(txtWachtwoord.Text);
            VAR PASSW =通过;
            BLLstudent.checkLogin(loginNr,PASSW);
            的Response.Redirect(学生/ s_procedure_goedkeuring.aspx);
        }
        赶上(例外前)
        {
            lblFeedback.Text = Ex.Message;
        }
    }
}
 

解决方案

.NET会话状态在presentation层处理的,虽然它是在一个网络工作者进程中运行的任何业务逻辑访问(注意,是也是出于过程中会话状态,但同样是从presentation层管理)。这是很少很好的做法,与会话的presentation层以外的互动。

在业务层,会话可以访问:

  System.Web.HttpContext.Current.Session
 

在大多数网络实体(页面,控制,查看),也就是对会话

引用

Session是一个基于密钥的集合;你把一个值与一个键,您检索相同值的关键。

 保护覆盖无效的OnLoad(EventArgs的发送)
{
    会话[富] =酒吧;
    字符串valueFromSession =会话[富]的ToString()。
}
 

Hi I'm creating a login form from scratch in c# using 3 tiers. I've managed to build a working form that checks if the user data is correct. If he filled in the wrong data he'll get a message. But now I need to create a session to store the id.

I've searched the web and they say you have to add Session["sessionName"]= data, but if I type Session["userId"]=s.studentNummer he doesn't recognize anything. Is it better to put the sessions in the DAL or in the DLL? I wanted to write it in the DAL (function checkLogin). Can somebody please help me?

Here's my code:

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;
        }
    }
}

解决方案

.NET session state is handled in the presentation tier, although it is accessible in any business logic running in a web worker process (note that there is also out of process session state, but that too is managed from the presentation tier). It is rarely good practice to interact with session outside of the presentation tier.

In the business tier, session can be accessed with:

System.Web.HttpContext.Current.Session

Inside most web entities (Page, Control, View) it is simply referenced by Session.

Session is a key-based collection; you put a value in with a key, and you retrieve the same value with a key.

protected override void OnLoad( EventArgs e )
{
    Session["foo"] = "bar";
    string valueFromSession = Session["foo"].ToString();
}

这篇关于在C#创建会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆