-2

With Visual Studio how to read username from login.aspx in homepage.aspx in a div?

Matt
  • 14,906
  • 27
  • 99
  • 149
  • 5
    Hi and welcome to SO, please read the [how to ask](http://stackoverflow.com/help/how-to-ask) guidelines and include your code to make a clear and easy to reproduce example. – I.G. Pascual May 16 '16 at 11:56
  • if you mean to access username in homepage.aspx after redirecting from loginpage.aspx. you can create a session variable having the username as variable in loginpage.aspx after successful authentication. Session["username"]=txtName.Text; – riteshmeher May 16 '16 at 12:14

1 Answers1

0

If you want to read the UserName of user that has logined successfully. So you can use Session.

ASP.NET session state enables you to store and retrieve values for a user as the user navigates ASP.NET pages in a Web application

In login page, after the user interd username save it to Session

string username = txtbx.Text;
Session["SessionName"] = username;

And in every page you can get it

string username = Session["SessionName"] .ToString();

More information :

https://msdn.microsoft.com/en-us/library/ms178581.aspx

mohsen
  • 1,763
  • 3
  • 17
  • 55
  • Thank you! But in the homepage string username = Session["username"].ToString(); gives error: "System.NullReferencesException – Daniele Botta May 16 '16 at 12:33
  • My code work correctly for me – mohsen May 16 '16 at 12:42
  • How debug and trace c# application https://support.microsoft.com/en-us/kb/815788 – mohsen May 16 '16 at 12:49
  • Let keep question simple.may be your problem is here http://stackoverflow.com/questions/1382791/what-should-i-do-if-the-current-asp-net-session-is-null – mohsen May 16 '16 at 12:56
  • It's perfect!!! Thank you!!!! – Daniele Botta May 16 '16 at 14:17
  • With Session can i limit the number of the session? How? Thank you – Daniele Botta May 26 '16 at 13:42
  • Why do you want to limit it ? When you are programmer so it depends on how much you use Session.how many data you can save on depending on how many ram of server .for example if you save one byte on session when user logined so if 1000 user is online on your site it gives 1000 * 1 = 1 Kbytes .I think it is not possible to limitations on number of variables of session – mohsen May 26 '16 at 14:01
  • More information http://stackoverflow.com/questions/2843237/asp-net-session-size-limitation and http://stackoverflow.com/questions/14970659/are-there-limits-for-session-variables-in-asp-net – mohsen May 26 '16 at 14:04
  • Excuse me are not explained well. If user login and go to homepage if he copy url and paste in new tab I want that he redirect to login and not access with same username – Daniele Botta May 26 '16 at 14:11
  • I think it is not possible. If user copy url and open same page or all pages of your site , it doesn't related to Session.one session just provide some memory of ram for each user.if user open many pages so why his variables (user name and ...)doesn't copy .so don't worry about it.instead you must study and learn about session – mohsen May 26 '16 at 14:47