Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Current User

Status
Not open for further replies.

prgmrgirl

Programmer
Feb 12, 2004
119
US
Oguys, I have done this before and of course, no longer have the code so I don't remember how I did this. We have some users who, once connected to our intranet, want to use a page. I would like to simply get the name of the user so that I can display "Welcome UserName" at the top of the page. Using either client-side or server side script, how can I get the current user?

Thanks a million!
prgmrgirl
 
[tt]User.Idnentity[/tt] works from a page class. if you want to reference the username from a none web object use [tt]System.Security.Principle.WindowsIdentity.GetCurrent();[/tt]

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
How about
[tt]System.Environment.UserName[/tt]?

________________________________________________________
Zameer Abdulla
Help to find Missing people
 
Thanks guys, I'll give these a try!

prgmrgirl
 

A question:

Using GetCurrent.Name I am getting this: JSMITH\ASP

as opposed to the expected DOMAIN\JSMITH.

Where exactly is this username coming from?

Thanks!
prgmrgirl
 
web.config
Code:
<configuration>
   ...
   <system.web>
      ...
      <authentication mode="Windows"/>
      <identity impersonate="true"/>
   </system.web>
</configuration>

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi Jason,

Thanks for the reply. Tried to do that but then I get NO username back at all. [sadeyes]

prgmrgirl
 
the client's browser must also have Windows Integrated Authentication enabled. Although I thought this was "on" by default.

open IE
Tools>Options>Advanced
mark the check box with a title similar to Windows Integrated Authentication.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
it all depends on how they login into the Intranet...

what about looking at the server variables?
are user login details stored in a DB?
is there an active session that you can lookup against?
do they use network logins? ( in which case you will be able to use the config as shown above)





 
Yeah, it is and mine has been on!

LOL

Any ideas what the heck could be causing this one?

prgmrgirl
 
Hi Nick, thanks for joining the lunacy!

They use a network login. The users would already be crawling around the network doing other things. It's just a matter of finding out who exactly is on the page at that moment. What I would like to have is their network login (i.e. DOMAIN\USERNAME). [wink]

cheers!

prgmrgirl

 
I got it to work this way:

1) Control Panel
2) IIS
3) Right-click web site
4) Properties
5) Directory Security
6) Uncheck Anonymous Access
7) OK

On the client form I ran this code:

Code:
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
        WindowsPrincipal principal = (WindowsPrincipal)Thread.CurrentPrincipal;
        WindowsIdentity identity = (WindowsIdentity)principal.Identity;

        Response.Write(identity.Name.ToString());
 
You need to set these references:

using System.Threading;
using System.Security.Permissions;
using System.Security.Principal;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top