Link to home
Start Free TrialLog in
Avatar of sysadmin2
sysadmin2

asked on

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.
Avatar of Velio
Velio
Flag of South Africa image

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

Avatar of sysadmin2
sysadmin2

ASKER

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.
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.
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.

ASKER CERTIFIED SOLUTION
Avatar of Velio
Velio
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Forced accept.

Computer101
EE Admin