String Writer

my tech stuffs…

Posts Tagged ‘LDAP

Get SID from LDAP Account Name

leave a comment »

During my migration we came across a situation where we need SID but all we had only LDAP user name. So after few trail and error googling, below code snippet helped us to get SID from LDAP user name.

/// <summary>
/// Gets the name of the SID by account.
/// <summary>
/// <param name="accountName"> Name of the account.</param>
/// <returns>SID</returns>
public string GetSIDByAccountName(string accountName)
{
   //name should be in the format of domain\username
   WindowsIdentity userIdentity = new WindowsIdentity(accountName);
   if (userIdentity == null)
      throw new Exception(string.Format("{0} : User does not exists", accountName));

   return userIdentity.User.AccountDomainSid.ToString();
}

Output : S-1-5-21-789336058-507921405-854245398-9938

Written by visvabalaji

August 13, 2009 at 7:02 am

Posted in LDAP

Tagged with , ,

Follow

Get every new post delivered to your Inbox.