W
Will .H
Guest
Hi there,
I have a question is regarding convert AD user SID to NTAccount (DOMAIN\User Account.). My environment is Windows 2016 server (LDAP Path: dev.testing.pri; Domain: DEVTST) and one testing program lives in another client (Windows 2019). my code is like the following:
using (DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://" + ldapPath, account, passWord, AuthenticationTypes.Secure))
{
var filter = "(userPrincipalName=" + emailAccount + ")";
using (DirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry, filter))
{
directorySearcher.PropertiesToLoad.AddRange(_properties);
using (System.DirectoryServices.SearchResultCollection results = directorySearcher.FindAll())
{
var properties = from p in results[0].Properties.OfType<DictionaryEntry>()
let values = (p.Value as ResultPropertyValueCollection).OfType<object>()
orderby p.Key
select new { Name = p.Key, Value = String.Join(", ", values) };
foreach (var _prop in properties)
Console.WriteLine($"{_prop.Name} = {_prop.Value}");
var prop = (byte[])results[0].Properties["objectsid"][0];
var sid = new SecurityIdentifier(prop, 0);
var ntaccount = sid.Translate(typeof(NTAccount));
var accountName = ntaccount.ToString();
}
Console.WriteLine(GetCurrentDomain.GetLDAPAttributes<string>(user.Properties["displayName"]));
Console.WriteLine(GetCurrentDomain.GetDomain(user));
Console.WriteLine(GetCurrentDomain.GetDomain(user) + "\\" + GetCurrentDomain.GetLDAPAttributes<string>(user.Properties["samAccountName"]));
Console.ReadLine();
}
}
var ntaccount = sid.Translate(typeof(NTAccount)); --> error comes up, the error is like the following: does anyone have any idea on it?? Thanks
Some or all identity references could not be translated. at System.Security.Principal.SecurityIdentifier.Translate(IdentityReferenceCollection sourceSids, Type targetType, Boolean forceSuccess) at System.Security.Principal.SecurityIdentifier.Translate(Type targetType) at Cons.Sample.Program.Main(String[] args) in C:\Users\Administrator\source\repos\Cons.Sample\Cons.Sample\Program.cs:line 59 mscorlib
Hi there, if you found my comment very helpful then please | Propose as answer | . Thanks and Regards.
Continue reading...
I have a question is regarding convert AD user SID to NTAccount (DOMAIN\User Account.). My environment is Windows 2016 server (LDAP Path: dev.testing.pri; Domain: DEVTST) and one testing program lives in another client (Windows 2019). my code is like the following:
using (DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://" + ldapPath, account, passWord, AuthenticationTypes.Secure))
{
var filter = "(userPrincipalName=" + emailAccount + ")";
using (DirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry, filter))
{
directorySearcher.PropertiesToLoad.AddRange(_properties);
using (System.DirectoryServices.SearchResultCollection results = directorySearcher.FindAll())
{
var properties = from p in results[0].Properties.OfType<DictionaryEntry>()
let values = (p.Value as ResultPropertyValueCollection).OfType<object>()
orderby p.Key
select new { Name = p.Key, Value = String.Join(", ", values) };
foreach (var _prop in properties)
Console.WriteLine($"{_prop.Name} = {_prop.Value}");
var prop = (byte[])results[0].Properties["objectsid"][0];
var sid = new SecurityIdentifier(prop, 0);
var ntaccount = sid.Translate(typeof(NTAccount));
var accountName = ntaccount.ToString();
}
Console.WriteLine(GetCurrentDomain.GetLDAPAttributes<string>(user.Properties["displayName"]));
Console.WriteLine(GetCurrentDomain.GetDomain(user));
Console.WriteLine(GetCurrentDomain.GetDomain(user) + "\\" + GetCurrentDomain.GetLDAPAttributes<string>(user.Properties["samAccountName"]));
Console.ReadLine();
}
}
var ntaccount = sid.Translate(typeof(NTAccount)); --> error comes up, the error is like the following: does anyone have any idea on it?? Thanks
Some or all identity references could not be translated. at System.Security.Principal.SecurityIdentifier.Translate(IdentityReferenceCollection sourceSids, Type targetType, Boolean forceSuccess) at System.Security.Principal.SecurityIdentifier.Translate(Type targetType) at Cons.Sample.Program.Main(String[] args) in C:\Users\Administrator\source\repos\Cons.Sample\Cons.Sample\Program.cs:line 59 mscorlib
Hi there, if you found my comment very helpful then please | Propose as answer | . Thanks and Regards.
Continue reading...