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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.