directoryservices

FindByIdentity - performance differences

不羁岁月 提交于 2019-12-04 22:57:43
The following code works fine from a variety of machines on our domain. var context = new PrincipalContext(ContextType.Domain); var principal = UserPrincipal.FindByIdentity(context, @"domain\username") However, if I run this similar code on a machine that is not on a domain, it works but the FindByIdentity line takes 2+ seconds. var context = new PrincipalContext(ContextType.Machine); var principal = UserPrincipal.FindByIdentity(context, @"machinename\username") Can this performance difference be addressed by supplying special parameters to the PrincipalContext constructor and/or the

UserPrincipal.GetGroups fails with unknown error

让人想犯罪 __ 提交于 2019-12-04 22:34:12
I am trying to get all Active Directory groups for a user, with the following code: private static IEnumerable<string> GetGroupNames(string userName) { using (var context = new PrincipalContext(ContextType.Domain)) { using (var userPrincipal = UserPrincipal.FindByIdentity(context, userName)) { var groupSearch = userPrincipal.GetGroups(context); var result = new List<string>(); foreach (var principal in groupSearch) { Log.LogDebug("User {0} is member of group {0}", userPrincipal.DisplayName, principal.DisplayName); result.Add(principal.SamAccountName); } return result; } } } This code correctly

How to use the objectGUID get a DirectoryEntry?

痴心易碎 提交于 2019-12-04 16:35:30
I know ,we can get a DirectoryEntry like this: string conPath = "LDAP://10.0.0.6/DC=wds,DC=gaga,DC=com"; string conUser = "administrator"; string conPwd = "Iampassword"; DirectoryEntry de = new DirectoryEntry(conPath, conUser, conPwd, AuthenticationTypes.Secure); and we can change a user's password like this: DirectorySearcher deSearch = new DirectorySearcher(); deSearch.SearchRoot = de; deSearch.Filter = String.Format("sAMAccountName={0}", "xumai"); SearchResultCollection results = deSearch.FindAll(); foreach (SearchResult objResult in results) { DirectoryEntry obj = objResult

How do I add a user to AD using System.DirectoryServices.AccountManagement?

≡放荡痞女 提交于 2019-12-04 14:50:57
Using the .net 3.5 framework and C# I'm trying to add a new user to AD from C# and can't find any examples. I see that the PrincipalCollection object has an overloaded 'add' method but can't seem to figure out how it works. Can anyone help? How create a new user object, add it into AD. Secondly, the user that will be adding in new people may not actually have the security to do this. Is there a way that I can impersonate another user account that will have permissions and add the account that way? You can add a user like this: using (var context = new PrincipalContext(ContextType.Domain))

Using StartTLS with LDAP from System.DirectoryServices

前提是你 提交于 2019-12-04 14:38:48
问题 I'm trying to connect to an LDAP server which requires StartTLS, but having no luck - whenever I use either the SessionOptions.StartTransportLayerSecurity(..) or set SessionOptions.SecureSocketLayer to true, I get exceptions. Here's the code I'm using: using (var connection = new LdapConnection(new LdapDirectoryIdentifier(config.LdapServer, config.Port, false, false))) { connection.SessionOptions.ProtocolVersion = 3; connection.Credential = new NetworkCredential(config.BindDN, config

How do I use BER encoding with object System.DirectoryServices.Protocols.BerConverter.Encode(“???”, myData)

纵然是瞬间 提交于 2019-12-04 14:30:38
问题 I need to encode and decode BER data. .NET has the class System.DirectoryServices.Protocols.BerConverter The static method requires me to enter a string in the first parameter as shown below byte[] oid = { 0x30, 0xD, 0x6, 0x9, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0xD, 0x1, 0x1, 0x1, 0x5, 0x0 }; // Object ID for RSA var result2 = System.DirectoryServices.Protocols.BerConverter.Decoding("?what goes here?", oid); BER encoding is used in LDAP, Certificates, and is commonplace in many other formats. I'll

How to list all computers and the last time they were logged onto in AD?

心不动则不痛 提交于 2019-12-04 14:03:43
问题 I am trying to retrieve a list of Computer Names and the date they were last logged onto from Active Directory and return them in a datatable. Getting the names is easy enough but when I try to add the "lastLogon" or "lastLogonTimestamp" like shown below, the only values I get for the lastLogonTimestamp is "System._ComObject" public DataTable GetListOfComputers(string domainName) { DirectoryEntry entry = new DirectoryEntry("LDAP://DC=" + domainName + ",DC=com"); DirectorySearcher search = new

Authentication Types when switching from System.DirectoryServices to DirectoryServices.Protocols

孤街浪徒 提交于 2019-12-04 12:54:01
I need to know the equivalent AuthType values from AuthenticationTypes to migrate from S.DS to S.DS.P code. I am rewriting an LDAP connection module that currently uses the System.DirectoryServices namespace. To increase compatibility with non-ActiveDirectory servers, I am trying to rewrite all of the code to use System.DirectoryServices.Protocols (as per the suggestion in "The .NET Developer's Guide to Directory Services Programming). Everything is going smoothly except for the transition between using the AuthenticationTypes enumeration to the AuthType one used by SD.Protocols. I need to

How to set a binary attribute when using a AccountManagement Extension Class?

情到浓时终转凉″ 提交于 2019-12-04 11:45:54
I am using a custom class to expose some custom schema in Active Directory . I am storing a binary blob, per the project requirements this data must be stored in the AD, I can not use a external store (I would if I could). When I create the user it stores the blob fine. I also can retrieve the blob back out fine too and get all my data. The issue is if I need to update the value and I am getting errors Small example program: using System; using System.DirectoryServices.AccountManagement; namespace SandboxConsole40 { class Program { static void Main(string[] args) { using(var context = new

ASP.NET MVC Windows Authentiaction and DirectoryServices - Get Mail Address of the current user throws an InvalidCastException

℡╲_俬逩灬. 提交于 2019-12-04 10:10:41
I am using ASP.NET MVC 4 and Windows Authentication. When I am using VisualStudio everything works fine, but when I deploy my site an exception is thrown. var emailAddress = UserPrincipal.Current.EmailAddress; throws: Unable to cast object of type 'System.DirectoryServices.AccountManagement.GroupPrincipal' to type 'System.DirectoryServices.AccountManagement.UserPrincipal'. The rest works fine. Users can authenticate and I can get the users name etc. EDIT: I enabled Impersonation on IIS. Now I get the following exception: [DirectoryServicesCOMException (0x80072020): An operations error occurred