Archive for August 2009
Get SID from LDAP Account Name
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
