Link to home
Create AccountLog in
Avatar of crickpaolo
crickpaolo

asked on

Getting the Windows Login name in ASP.NET

I am running this chunk of code on my ASP.NET page:

Dim networkuser As WindowsIdentity
Dim networklogon As String
Dim arrnetworklogon As Array
Dim loginname As String
       
 'get network user data
networkuser = WindowsIdentity.GetCurrent
networklogon = networkuser.Name 'this will return the domain\network login
       
arrnetworklogon = Split(networklogon, "\")
loginname = arrnetworklogon(UBound(arrnetworklogon)) 'extract the last part of the network logon

When testing the application under my local machine, it is working fine. loginname shows my windows login.

When I moved it to the production server, loginname becomes "NETWORK SERVICE".  Do I need to set up anything on IIS in order to make it work?

The application I'm building is an intranet application.  


         
ASKER CERTIFIED SOLUTION
Avatar of skiltz
skiltz
Flag of New Zealand image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of crickpaolo
crickpaolo

ASKER

Thanks skiltz.   I googled "user.indentity.name" and I couldn't find a clean example of how to use it.  Do you have any code snippets that will get the windows login using user.indentity.name?  I am not sure what namespaces to include, how to declare it etc.

 
Ok, I found out how to use user.identity.name by modifying my code like this:

Dim networklogon As String
Dim arrnetworklogon As Array
Dim loginname As String
       
'get network user data
networklogon = User.Identity.Name
arrnetworklogon = Split(networklogon, "\")
loginname = arrnetworklogon(UBound(arrnetworklogon)) 'extract the last part of the network logon

When I run it, I am getting a blank  for logginname.  What am I doing wrong?

Thanks.
   
Do you get a value for networklogon or User.Identity.Name??

if so

  Dim loginName As String
        Dim lgDash As Long
        lgDash = User.Identity.Name.IndexOf("\")
        loginName = Replace(User.Identity.Name.Substring(lgDash), "\", "")
Nope. I get a blank on User.Identity.Name.

I did find another solution though. Here's my code:

Dim networklogon As String
Dim arrnetworklogon As Array
Dim loginname As String
       
'get network user data
networklogon = Request.ServerVariables("LOGON_USER")
arrnetworklogon = Split(networklogon, "\")
loginname = arrnetworklogon(UBound(arrnetworklogon)) 'extract the last part of the network logon

In order for it to work, I tweaked IIS and set the "Allow Anonymous Access" to false, and selected Windows security.

I will giving you the points though because you presented a possible alternative solution. I'm sure User.Identity.Name works, and I probably just had to modify the Web.config file, or some IIS parameters....