Learn how to a build a cloud-first strategyRegister Now

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

vb.net - get login id (Domain/userid)

Hi Everyone,

I am building an application on visual web developer express 2005.  This application has a workflow in which after data is entered into the database, the data will be sent to appropriate parties for approval.  I would like to get the user.id of the person who is doing the approving.  I've tried a few codes such as:

     Request.ServerVariables("LOGON_USER")

     Environment.UserDomainName & "\\" & Environment.UserName

     My.User.Name

But none of them is working.

I would like the domain name to come up as ADP/username

Can someone help me with the code for this?  

Thanks!
0
sarah_siu
Asked:
sarah_siu
  • 8
  • 6
  • 2
2 Solutions
 
CluskittCommented:
Use:
User.Identity.Name
0
 
CodeCruiserCommented:
Are you using Windows Authentication?
0
 
sarah_siuAuthor Commented:
Thanks for both of your response.  

CodeCruiser: Can you clarify what you mean by windows authentication?  There is currently no userid/password set for this application.  Users have an active domain ID which they log in to their computers with.  Not sure if that answers your question?

Cluskitt: I tried your code.  For my test, I have a label on my webpage and i've set it so that on page load, it should retrieve the user id.  It works when i'm in the visual web developer and i run the code through the application.  But once i save the file into my web server and open the website in googlechrome, I get a blank field...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CluskittCommented:
Are you sure that your user is authenticated? Try this:
If User.IsAuthenticated Then
  Label1.Text = User.Identity.Name
Else
  Label1.Text = "User not authenticated"
End If

I always use the above methods and they work as advertised.
0
 
sarah_siuAuthor Commented:
Hi Cluskitt,

I tried using the code provided and this is the error that I got:

Error      1      'IsAuthenticated' is not a member of 'System.Security.Principal.IPrincipal'.      

Should I be doing something to the form to get this to work?  I am using an older version of visual web developer (Visual web developer 2005 Express Edition)

Thanks!
0
 
CluskittCommented:
Sorry, that was my typo. It's User.Identity.IsAuthenticated.
0
 
sarah_siuAuthor Commented:
Hi Cluskitt, thanks for the quick reply.  I used your code and it worked but it says "user not authenticated".  As our company requires users to log-onto their computer through an active domain, I would think that we are 'authenticated'.  would you mind explaining to me exactly what being authenticated means?

thanks
0
 
CluskittCommented:
Ah, then we're talking of different things. I was talking about .Net authentication. That is, logging on to the application, not the computer. Taken from MSDN, you might want to try this:
Function GetUserName() As String
    If TypeOf My.User.CurrentPrincipal Is _
    Security.Principal.WindowsPrincipal Then
        ' The application is using Windows authentication.
        ' The name format is DOMAIN\USERNAME.
        Dim parts() As String = Split(My.User.Name, "\")
        Dim username As String = parts(1)
        Return username
    Else
        ' The application is using custom authentication.
        Return My.User.Name
    End If
End Function

Open in new window

If this still returns nothing, you might want to change the returns to debug which is being returned.
0
 
sarah_siuAuthor Commented:
Hi Cluskitt, when I used your code, I get this error :

Error      1      'Security' is ambiguous, imported from the namespaces or types 'System.Web,

Thanks!
0
 
CluskittCommented:
The namespace on MSDN is Microsoft.VisualBasic.ApplicationServices. So I guess you should try Microsoft.VisualBasic.ApplicationServices.Security.Principal.WindowsPrincipal. However, I'm not sure how this plays with whatever authentication you have defined on web.config.
I personally just don't bother with windows auth and force a logon for the web app. Maybe that's a method you might try?
0
 
CluskittCommented:
Actually, the namespace would be System, but that wouldn't work. I tried a few experiments on my site, and this works:
Partial Class Page1

  Declare Function GetUserName Lib "advapi32.dll" Alias _
       "GetUserNameA" (ByVal lpBuffer As String, _
       ByRef nSize As Integer) As Integer

  Public Function GetUserName() As String
    Dim iReturn As Integer
    Dim userName As String
    userName = New String(CChar(" "), 50)
    iReturn = GetUserName(userName, 50)
    GetUserName = userName.Substring(0, userName.IndexOf(Chr(0)))
  End Function

  Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    Label1.Text = GetUserName()
  End Sub
End Class

Open in new window

0
 
sarah_siuAuthor Commented:
It is so strange.  It works perfectly when I debug it on visual web developer but once I go into the website via the web, it brings back 'NETWORK SERVICE' only....I'll try to figure out what's going on...but your code seems to work fine in the application.  Just doesn't transfer to my website over the web....
0
 
CluskittCommented:
How are you running the application? I think that what it's returning is the owner of the process that is running the application. Usually, that would be IE/Firefox/Chrome and the owner would be the logged user. However, some processes are owned by NETWORK SERVICE, SYSTEM or similar.
0
 
CodeCruiserCommented:
0
 
sarah_siuAuthor Commented:
Hi Everyone,

Code cruiser's code helped me to disable the anonymous feature on the website and Cluskitt helped me to get the ID.  Really appreciate both of your help and sorry for the delay.  I was away and didn't have a chance to look at this till now.  Really appreciate your time and help.
0
 
CluskittCommented:
No problem. Glad to help and that everything's working fine.
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

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