简体   繁体   中英

set session in page_load

I've a page width this code in a page a called Log.aspx:

protected void Page_Load(object sender, EventArgs e)
{

    Session.Abandon();
    Session.Clear();
    string username = "a";
    if (!String.IsNullOrEmpty(username))
    {
        db_user = db.CBR_User
            .Include("CBR_MissioniGruppi")
            .Include("Anagrafica_Dipendente.Extra_dipendente")
            .Include("Anagrafica_Dipendente.Dati_Dipendente")
            .FirstOrDefault(p => p.Anagrafica_Dipendente.Extra_Dipendente.Codice_Fiscale.Equals(username, StringComparison.OrdinalIgnoreCase));
        Session["User"] = db_user;
        Session["t_admin"] = null;
        Response.Redirect("Default.aspx");
    }
}

The problem is that if i launch 2 times the page with 2 username differents in Default.aspx page the second time a read the variable Session["User"] saved in the first time.

es.

1)Launch Log.aspx with username = "a"
-> the session["User"] in Default page has the user with username a
2) Launch Log.aspx with username = "n"
-> the session["User"] in Default page has the user with username a

Why?

I've seen that if i put a button in log page and i excute the procedure on click event, it works.

How can i do to fix it?

thanks

It's not not completely clear what you're asking here. Looking at your code, you're hard coding the value of username to 'a', so it will always be 'a'.

string username = "a"; 

If that's not your problem, I would imagine you have some sort of race condition where your multiple reads/writes to the Session are causing conflict.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM