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.
LVL 1
sysadmin2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Computer101Commented:
Forced accept.

Computer101
EE Admin
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.