Beginner questions on C#

I have two very simple pages that seem to work very well - one is a login page, where the user enter's their email, which gets validated against a set of parameters, then set into a session (if valid). The user is then passed to the second page in the process, which would be the "home" page of the project.

The second "home" page has a small piece of code to make sure the user has in fact logged in. If they haven't (like the book marked page two to bypass login), they are redirected to page one.

In theory.

For some reason, it doesn't work.

Page one's code:
public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //nothing yet
    }

    public static bool isEmail(string inputEmail)
    {
        string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((domain1\.com)|(domain2\.com))$";
       
        Regex re = new Regex(strRegex);

        if (re.IsMatch(inputEmail))
            return (true);
        else
            return (false);
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (!isEmail(this.txtEmailAddress.Text))
        {
            lblErrorMessage.Text = this.txtEmailAddress.Text + " is not a valid email address.";
        }
        else
        {
            lblErrorMessage.Text = "Email Address is valid.";
            Session.Add("UserEmail",this.txtEmailAddress.Text);
            Response.Redirect("Default.aspx");
        }
    }
}

Page two's code:
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string strUserEmail = (string)(Session["UserEmail"]);

        if (isLoggedIn(strUserEmail))
        {
            Response.Redirect("login.aspx");
        }
        else
        {
            lblEmailDisplay.Text = strUserEmail;
        }
    }

    public static bool isLoggedIn(string userEmailString)
    {
        if (userEmailString == "") {
            return (false);
        } else {
            return (true);
        }
    }
}



The issue I get is this: the session object doesn't seem to clear itself when the browser closes, so the user is always "logged in". If I go straight to default.aspx, the address I used once to log in still displays. OR I get error condition #2, where the login page sends the info to page two, but page two ALWAYS redirects back.

Help!
LVL 3
Paul KahlAsked:
Who is Participating?
 
quoclanCommented:
Session object will be destroyed when the browser close.
You just modify little like this :

Page two's code:
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string strUserEmail = (string)(Session["UserEmail"]);  //if Session["UserEmail"] is not existed, strUserEmail = null

        if (!isLoggedIn(strUserEmail))             //! is added
        {
            Response.Redirect("login.aspx");
        }
        else
        {
            lblEmailDisplay.Text = strUserEmail;
        }
    }

    public static bool isLoggedIn(string userEmailString)
    {
        if (userEmailString == null) {           // compare userEmailString with null
            return (false);
        } else {
            return (true);
        }
    }
}

Or, you can make strUserEmail = "" if Sesssion["UserEmail"] is not existed by using Convert.ToString() like this
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string strUserEmail = Convert.ToString((Session["UserEmail"]));  //if Session["UserEmail"] is not existed, strUserEmail = ""

        if (!isLoggedIn(strUserEmail))             //! is added
        {
            Response.Redirect("login.aspx");
        }
        else
        {
            lblEmailDisplay.Text = strUserEmail;
        }
    }

    public static bool isLoggedIn(string userEmailString)
    {
        if (userEmailString == "") {           // preserved
            return (false);
        } else {
            return (true);
        }
    }
}

Hope this help you.
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.