Answered by:
Session state is not available in this context.

Question
-
User-712926555 posted
Hi,
I am using forms authentication
I need to get user by name with respect branch
In my global.ascx
protected void FormsAuthentication_OnAuthenticate(Object sender, FormsAuthenticationEventArgs e) { if (FormsAuthentication.CookiesSupported == true) { if (Request.Cookies[FormsAuthentication.FormsCookieName] != null) { try { //let us take out the username now string username = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name; Admin_AppUserBLL objAppusr = new Admin_AppUserBLL(); if (!String.IsNullOrEmpty(Session["BranchId"].ToString())) { objAppusr = Admin_AppUserBLL.GetAdmin_AppUserBLL(username, Convert.ToInt32(Session["BranchId"].ToString())); } else { objAppusr = Admin_AppUserBLL.GetAdmin_AppUserBLL(username); } //let us extract the roles from our own custom cookie username += "|" + objAppusr.BranchId + "|" + objAppusr.Id; //Let us set the Pricipal with our user specific details e.User = new System.Security.Principal.GenericPrincipal(new System.Security.Principal.GenericIdentity(username, "Forms"), objAppusr.RoleName.Split(';')); } catch (Exception ex) { throw ex; //somehting went wrong } } } }
In Login.aspx.cs
protected void btnLogin_Click(object sender, EventArgs e) { Session["BranchId"] = ddlBrachName.SelectedValue.ToString(); // my code here { FormsAuthentication.RedirectFromLoginPage(txtUsername.Text, false); Response.Redirect("~/Home.aspx"); }
It through an exception
Session state is not available in this context. How to pass branchId from login page to global.ascx?
Saturday, August 29, 2015 6:28 AM
Answers
-
User475983607 posted
same username with different branch. how to manage itThat's a good question. If usernames are not unique then you must have a way to identify each branch. How are you doing identifying a branch?
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, August 30, 2015 7:05 AM -
User1724605321 posted
Hi Hisanth ,
Have you tried HttpContext.Current.Session as mgebhard suggested? if you get the same error ,i think maybe at that time session state has been loaded .you could refer to link below for more details:
http://forums.asp.net/t/1058902.aspx?Questions+about+SessionState+in+Global+asax .
http://stackoverflow.com/questions/15134265/retrieving-session-in-global-asax
Best Regards,
Nan Yu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, September 1, 2015 1:33 AM
All replies
-
User475983607 posted
Try HttpContext.Current.Session["BranchId"].
I did not check but you might find Session is not available at this point in the page life cycle. Regardless, I would rework the code to remove the dependency on Session. You could place the BranchId inside the auth token or simple query the DB to get the ID.
Saturday, August 29, 2015 6:36 AM -
User-712926555 posted
simple query the DB to get the IDsame username with different branch. how to manage it
Saturday, August 29, 2015 6:53 AM -
User475983607 posted
same username with different branch. how to manage itThat's a good question. If usernames are not unique then you must have a way to identify each branch. How are you doing identifying a branch?
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Sunday, August 30, 2015 7:05 AM -
User1724605321 posted
Hi Hisanth ,
Have you tried HttpContext.Current.Session as mgebhard suggested? if you get the same error ,i think maybe at that time session state has been loaded .you could refer to link below for more details:
http://forums.asp.net/t/1058902.aspx?Questions+about+SessionState+in+Global+asax .
http://stackoverflow.com/questions/15134265/retrieving-session-in-global-asax
Best Regards,
Nan Yu
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, September 1, 2015 1:33 AM -
User-446933086 posted
Hi,
There are different ways to do this stuff.
My idea about this stuff is
HttpContext.Current.Request.Form.GetValues(ddlBrachName.UniqueID)[0]
ddlBrachName
How you will get ddlBrachName.UniqueID in global.ascx. you can code it or you can pass the index to get the value from request. Instead of keeping in session to just the get the value in Global session will not be good idea. HttpContext.Current contain all information which are required during the request time.
Wednesday, September 2, 2015 6:46 AM