[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1968
  • Last Modified:

How do I create a session variable in the global.asax file from the LoginName and then display it on my default.aspx page.

Thank you in advance for your help.

I am attempting to create a session variable "ULogon" in the Session_Start section of the global.asax file.  The particular is that I would like to assign the LoginName to the ULogon session variable.  I have attempted this many different ways, but I have not been successful.  The second part is displaying this session variable on the default.aspx page.   I just starting to learn ASP.NET and C# so please bear with me.
0
sysadmin2
Asked:
sysadmin2
  • 3
  • 2
1 Solution
 
VelioCommented:
what happens when you create a new website, add the Global.asax file and add the code below?

does it differ from your code and how?

if all you need is the username, you could use the System.Web.UI.Page.User property which all your pages will have.
in the Global.asax:
 
void Session_Start(object sender, EventArgs e) 
{
    // Code that runs when a new session is started
    Session["myVariable"] = "myValue";
 
}
 
in the Default.aspx.cs:
 
protected void Page_Load(object sender, EventArgs e)
{
    Literal value = new Literal();
    value.Text = Session["myVariable"].ToString();
    form1.Controls.Add(value);
}

Open in new window

0
 
sysadmin2Author Commented:
Velio, thank you for your help.  I think I need to make something clear about the words "LoginName" I used above.  This is the LonginName control that is in the tool box in Visual Web Developer 2008 Express Edition.  My site is on an intranet using Active Directory.  When I add the LoginName control to a page it clearly shows domain\user.  I want to get this value into the session variable "ULogon" so I can use it to display on a page.  What I did not include in the earlier description is that I also want to use the session variable in a database query to filter the results.  I knew if I could get it into the session variable I could do the database filter myself.  If I misled you on the inital post, I do appologize.  My main struggle is getting the LoginName control value into a session variable.  With that said, your code above does display "myValue" on the default page.  Does getting a session variable to display in asp.net require that much code?  In the old asp, a simple <%=Session("myVariable")%> would display a session variable.  Thanks again for your help.
0
 
VelioCommented:
ok, then the global.asax doesn't even come into play.

if you're using windows authentication, you don't even need a session variable,

use Page.User.Identity.Name to get the currently logged in user's name. every page in your website will have this, so it will be accessible everywhere on the site, and can be passed along to any database modules.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
sysadmin2Author Commented:
I thought this should be simpler because of Active Directory and the intranet context.  The reason I was using LoginName is that from what I have read, Page.User.Identity.Name is what LoginName gives you:
(http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.loginname.aspx).

My problem is getting this into a variable.  I may be making this too hard or I am just completely ignorant of the syntax.  To simplify, can you give me the code to put Page.User.Identity.Name or LoginName into a variable?  I was thinking I would need a session variable in the global.asax file so it would be an option when I setup the filter as I build the SQLDataConnector in the wizard.  If it can be done without the Session variable, I am all for it.  Thanks again.

0
 
VelioCommented:
to put it into a session variable, you can do this
Session["ULogin"] = Page.User.Identity.Name;

you can then use that session variable by doing something like:
Session["ULogin"].ToString()

but seeing as how the Page.User.Identity.Name variable will be available accross all your pages, I'm not sure there's a need to put it in a session variable. For example, let's say you have two or more pages in your site

if you go the Session route, you'll have to ensure that the session variable is properly initialized before you use it, on every page:
if (Session["ULogon"] == null)
  Session["ULogon"] = Page.User.Identity.Name;
(or something along those lines)

whereas Page.User.Identity.Name is already available, as long as the user has logged in (which will happen automatically using Active Directory/Windows authentication)

The LoginName control will display the name, but it seems there's no way to actually retrieve that name back from the control.

I hope this helps you, sorry if i'm misunderstanding you :)
0
 
Computer101Commented:
Forced accept.

Computer101
EE Admin
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now