Session Variable not accessible c#/Visual Web Developer

I am having a problem with the code below. I simply want to go from a shopping cart page to a select credit card page, and redirect if not logged in. The problem is that even after login the cc page still can't catch the session user name, even though it is present on the login label.

I have attached the code that stores the session variable on the login page, and the if statement that redirects. I am not too familiar with c#, so it is probably something I am doing wrong... Thanks!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class login : System.Web.UI.Page
{
    protected void Login1_LoggedIn(object sender, EventArgs e)
    {
        //user is logged in, store username in a session variable
        Session["userName"] = Login1.UserName;
        //Get the userID of this user and store it in a session variable
        MembershipUser usr = Membership.GetUser((string)Session["userName"]);
        Session["userID"] = usr.ProviderUserKey;
        
    }
}

Open in new window

sukotto100Asked:
Who is Participating?
 
karakavCommented:

I think the problem you are facing is the user of Response.Redirect. Some times it throws a ThreadAborException. So what I do normally is to catch the error and do nothing inside the cath block.
        try
        {
            if (Session["userName"] == null)
            {
                Response.Redirect("checkoutLogin.aspx", true); //<---If I comment out, it works
            }
        }
        catch (System.Threading.ThreadAbortException)
        {
        }
As well there is a proper way of Checking if a user is authenticated. You just need to check the value of HttpContext.Current.User.Identity.IsAuthenticated property
And to get the user name: get the value of  HttpContext.Current.User.Identity.Name property
0
 
sukotto100Author Commented:
SelectCC.aspx.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SelectCC : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //If user is not login, redirect the user to the login page. 
        if (Session["userName"] == null)
        {
            Response.Redirect("checkoutLogin.aspx", true); //<---If I comment out, it works
        }
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //store in a session variable the credit card number selected by user
        //and then redirect the control of execution to the ShowPurchasedSongs.aspx
        Session["cc_no"] = GridView1.SelectedDataKey.Value;
        Response.Redirect("PurchasedSongs.aspx", true);
    }
}

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.