Link to home
Create AccountLog in
Avatar of newbieal
newbiealFlag for United States of America

asked on

asp .net: get user's name from AD

I have the following code snippet in my page_load of the default page, however I'd like to display the user's name not nt login...how is this possible?

  Dim userName As String = usr.Substring(usr.LastIndexOf("\") + 1)
            Label1.Text = "Welcome " + userName + "!"
Avatar of nsanga
nsanga

dim lintIndex as integer= usr.indexof("\")

if lintindex >=0 then
     Label1.Text = "Welcome " + usr.Substring(lintindex + 1) + "!"
else
      Label1.Text = "Welcome " + usr + "!"
end if
Avatar of newbieal

ASKER

This is not working for me.  The page doesn't load it just sits there (almost acts like its in an infinite loop).  I have this:


Dim usr As String = User.Identity.Name
               Dim lintIndex As Integer = usr.IndexOf("\")
 
                If lintIndex >= 0 Then
                    Label1.Text = "Welcome " + usr.Substring(lintIndex + 1) + "!"
                Else
                    Label1.Text = "Welcome " + usr + "!"
                End If

Open in new window

did you try

Label1.Text = "Welcome " + usr + "!"

commenting other code..........what is the value displayed?
I only have these two lines and the page displays: domainname\user
Dim usr As String = User.Identity.Name
Label1.Text = "Welcome " + usr + "!"

Open in new window

try adding try catch

try

Dim usr As String = User.Identity.Name
               Dim lintIndex As Integer = usr.IndexOf("\")
 
                If lintIndex >= 0 Then
                    Label1.Text = "Welcome " + usr.Substring(lintIndex + 1) + "!"
                Else
                    Label1.Text = "Welcome " + usr + "!"
                End If

  Catch ex As Exception
  Label1.Text = "Error : " + ex.message
        End Try
I do have try/catch in place but doesn't throw any errors.  Just adding the code the way you have it gives the username part of domainname\username, but that's not what I want. I want the full name of the person.  Would this require accessing AD?
where is full name stored..........what is AD
Active Directory...isn't the user's full name stored there along with the nt login?  nt login meaning the username in domain\username.
Use this code to get the firstname and lastname from the active directoy and then  use ur code to display it in ur format

string ldapPath = "LDAP://cn=users,ou=org_name,dc=domain,dc=com";
DirectoryEntry currentUser = new DirectoryEntry(ldapPath);
 
if(currentUser != null)
{
    string firstName = currentUser.Properties["givenName"].Value.ToString();
    string lastName = currentUser.Propeties["sn"].Value.ToString();
}
I translated it into VB as follows:

There are two issues, I cannot use the is not (or <>) it says: it's not defined for these types, and secondly, when I comment out the if statement and just have the fullName=currentUser.... line then I get this error when running the page:
Error : The given path's format is not supported.
Dim ldapPath As String = "LDAP://cn=users,ou=org_name,dc=domain,dc=com"
           Dim currentUser As IO.DirectoryInfo = New IO.DirectoryInfo(ldapPath)
            Dim fullName As String
            If (currentUser <> DBNull.Value) Then
                fullName = currentUser.FullName.ToString()                
            End If

Open in new window

add your ldap connection string instead the one i posted.
In the if statement use currentuser<>""
ASKER CERTIFIED SOLUTION
Avatar of nsanga
nsanga

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