Getting a users SID

Last post 04-01-2008 9:41 AM by johram. 1 replies.

Sort Posts:

  • Getting a users SID

    03-27-2008, 12:11 PM
    • Loading...
    • all2neat
    • Joined on 12-06-2006, 5:37 AM
    • Baton Rouge, LA
    • Posts 52

    Hi,


    I have a form which will be used to log tech support calls. What happens is a user enters in a username and I want to return a firstname, lastname, and email address.

    I have this working if it gets the currently logged on user via Request.LogonUserIdentity.User and it returns the name and email address but not for the user calling in.

    I included a sample of what I'm trying to do but of course you can't go directlly from username to sid as shown in line one.

    1    Dim user As System.Security.Principal.SecurityIdentifier = txtUserName.Text.Trim 'error here .
    2    
    3    Dim dir As New DirectoryEntry("LDAP://<SID=" + SidToHex(user) + ">")
    4    dir = New DirectoryEntry("LDAP://" + DirectCast(dir.Properties("distinguishedName")(0), String), Nothing, Nothing, AuthenticationTypes.Secure Or AuthenticationTypes.ReadonlyServer)
    5    
    6    FirstName = dir.Properties("givenName")(0).ToString
    7    LastName = dir.Properties("sn")(0).ToString
    8    EmailAddress = dir.Properties("mail")(0).ToString
    9    
    
     
    New Orleans Hornets Season Ticket Holder!
  • Re: Getting a users SID

    04-01-2008, 9:41 AM
    Answer
    • Loading...
    • johram
    • Joined on 06-13-2006, 10:36 AM
    • Sweden
    • Posts 3,352
    • Moderator

    If you have the username of the calling user, then you should use DirectorySearcher with a filter including the sAMAccountName. There's no shortcut to jump from username to SID as you have already discovered. 

    DirectoryEntry root = new DirectoryEntry("DC=somedomain,DC=com");
    DirectoryEntry user = null;
    using (DirectorySearcher search = new DirectorySearcher(root))
    {
    	search.SearchScope = SearchScope.Subtree;
    	search.Filter = "(&(objectClass=user)(sAMAccountName=" + txtUserName.Text.Trim() + "))";
    	search.PropertiesToLoad.AddRange(new string[] { "givenName", "sn", "mail" });
    	SearchResultCollection searchResultColl = search.FindAll();
    	if (searchResultColl.Count == 1)
    	{
    		user = searchResultColl[0].GetDirectoryEntry();
    	}
    	else
    	{
    		// User not found
    	}
    }
    FirstName = user.Properties["givenName"][0].Value;
    ...

    I just realized you're probably working in VB.net. I wrote this code in C#, but I think you should be able to get the point with this. Good luck!
     

    If this post was useful to you, please mark it as answer. Thank you!
Page 1 of 1 (2 items)
Microsoft Communities
Page view counter