[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Beginner questions on C#

Posted on 2006-04-13
1
Medium Priority
?
215 Views
Last Modified: 2010-04-16
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!
0
Comment
Question by:Paul Kahl
1 Comment
 
LVL 2

Accepted Solution

by:
quoclan earned 2000 total points
ID: 16449388
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question