2

Hi I have problem with store user name in Session. On log on page I store user name to session. User input credentials on logon page and then is redirect on default page. I need in class Default access to variable store in session.

Logon.aspx

<script runat="server">
    void Login_Click(object sender, EventArgs e)
    {
                 Session["user"]  = tbUserName.Text;

                //You can redirect now.
                Response.Redirect(FormsAuthentication.GetRedirectUrl(tbUserName.Text, 
false));
}
</script>

secod main page is here:

Default.aspx-Default.aspx.cs

public partial class Default : Page, 
    IHttpHandler, System.Web.SessionState.IRequiresSessionState 
{
      string userName = (string) (Session["user"]);
}

but result is that userName is empty.

Web config is here:

<sessionState cookieless="true" regenerateExpiredSessionId="true" />

Thank you for advice.

2
  • In the second page, is that snippet in Page_Load? Commented Jun 21, 2011 at 12:51
  • You should post complete code to figure out the issue? Commented Jun 21, 2011 at 12:54

3 Answers 3

1

If the snippet is on the module level (as alluded to by Bala R in the comments) the session state has probably not been set yet. Try getting it in a method that is part of the page life cycle (like page_load, etc.).

Sign up to request clarification or add additional context in comments.

Comments

1

Why not get it directly from HttpContext HttpContext.Current.User.Identity.Name

Edit: I guess you are trying to get it at page level instead of in page_load or another place. That's why you are not getting a session value.

I hope below will work for you

 protected void Page_Load(object sender, EventArgs e)
 {
     string userName = (string) (Session["user"]);
 }

Comments

0

Update: This is incorrect.

You're losing the (cookieless) session when you redirect. Cookieless sessions are maintained in the URL, and are lost when you explicitly redirect, so there is nothing in the session on your second page.

1 Comment

According to Microsoft documentation, cookieless sessions are maintained unless the redirection URL is an absolute path, so a relative path should safely keep the session intact. That means it would depend on the redirection destination.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.