how to use Session for Login.aspx , create a session of user on the redirect page or on every page after authenticated login

therock_80
therock_80 used Ask the Experts™
on
hi , i 'm new to asp and .net stuff. i has problem with share session Login.aspx and every page of my website. For examples, when i was authenticated on Login.aspx page then i click on or redirect to other page, i still authenticated on these page
More infos : First i got problem that i create a session for a username on Login.apx page but i dont know how to share that session back to Homepage, and display like this "Welcome to, Username --- Logout"  on the top. I 'm working on C# language.  
Thanks for any help
I will accept any help if it make it work
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Try this..

once the user is successfully logged in, write the following code in your Login.aspx.cs code behind..

private void Submit_Click(..., ...)
{
     if (Login(username, password))
     {
          ...
          Session.Add("Username", username);
          ...
     }
     else
     {
          // display back to the user that login is incorrect.
     }
}

Now in other pages, you can retrieve the username with the help of the following code..

string username = Session["Username"];

I hope this helps and this is what you are looking for.

Good Luck..

Siddhartha

Author

Commented:
No, it did not work because i checked value of Session["Username"] is  empty on other page.

More infos:

I did login OK, i using SQL database  storing username and password
Then after i login already, i created a session for that user,
But when redirect to other page , i can not  retrieve value of Session["Username"] or any session
Thats what i did is same you (mehtas)  said

OK here code piece on Login.aspx:
private void LoginButton_Click(object sender, System.EventArgs e)
            {
                  if(Authenticate(UserNameTextBox.Text,PasswordTextBox.Text))
                  {                                                      
                        Session.Add("Username",UserNameTextBox.Text.ToString().Trim());
                        Response.Redirect("forums.aspx");                        
                  }            
            }
And the code on home page i retrieve the session:
                                             string Username = (string)Session["Username"];
                  output.Text = "\r\nUsername : "+ Username;

Ouput in the Ouput textbox did not display anything !!!!

waiting for any help now

Author

Commented:
More infos:
function:  Authenticate(string user, string pass) return bool and i test that it work OK
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Commented:
try,

Response.Redirect("forums.aspx", false);

Commented:
you know what, I just wrote a small code and without specifying "false", it works for me. Just wondering did you change any setting in the Web.config file.

Here is my code:

**************This is WebForm1**************
...
...
private void Button1_Click(object sender, System.EventArgs e)
{
    string txt = TextBox1.Text;

    Session.Add("Name", txt);
   
    Response.Redirect("WebForm2.aspx");
}
...
...

***********WebForm2.aspx***********

...
private void Page_Load(object sender, System.EventArgs e)
{
    TextBox1.Text = (string)Session["Name"];
}
...

And in TextBox1 of WebForm2, I get the string that I added in the Session.

Siddhartha

Commented:
You are using \r\nUsername: in your Output.Text.. in that case have you turned on MultiLine??? By default the textbox has only 1 line and with \n, you are trying to write in next line..

Siddhartha

Author

Commented:
still did not work i turn on Multiline of Output control aleady
It just display : " Username : " and nothing after that, and that is a big text box

Author

Commented:
damn it s because i used Zone Alarm,,,,damn damn damn it :))))
it works after i shut down this firewall
Anyway what if people login using firewall??????
So we can not have solution for that?????
Thanks for help anyway

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial