How to get the current user’s Active Directory details in C#

The “pre Windows 2000” name i.e. DOMAIN\SomeBody, the Somebody portion is known as sAMAccountName. So try: using(DirectoryEntry de = new DirectoryEntry(“LDAP://MyDomainController”)) { using(DirectorySearcher adSearch = new DirectorySearcher(de)) { adSearch.Filter = “(sAMAccountName=someuser)”; SearchResult adSearchResult = adSearch.FindOne(); } } someuser@somedomain.com.au is the UserPrincipalName, but it isn’t a required field.

How to programmatically change Active Directory password

You can use the UserPrincipal class’ SetPassword method, provided you have enough privileges, once you’ve found the correct UserPrincipal object. Use FindByIdentity to look up the principal object in question. using (var context = new PrincipalContext( ContextType.Domain )) { using (var user = UserPrincipal.FindByIdentity( context, IdentityType.SamAccountName, userName )) { user.SetPassword( “newpassword” ); // or user.ChangePassword( … Read more

Powershell: A positional parameter cannot be found that accepts argument “xxx”

Cmdlets in powershell accept a bunch of arguments. When these arguments are defined you can define a position for each of them. This allows you to call a cmdlet without specifying the parameter name. So for the following cmdlet the path attribute is define with a position of 0 allowing you to skip typing -Path … Read more

How do I get the first name and last name of the logged in Windows user?

If you’re using .Net 3.0 or higher, there’s a lovely library that makes this practically write itself. System.DirectoryServices.AccountManagement has a UserPrincipal object that gets exactly what you are looking for and you don’t have to mess with LDAP or drop to system calls to do it. Here’s all it’d take: Thread.GetDomain().SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); WindowsPrincipal principal = (WindowsPrincipal)Thread.CurrentPrincipal; … Read more

Built-in helper to parse User.Identity.Name into Domain\Username

This is better (easier to use, no opportunity of NullReferenceExcpetion and conforms MS coding guidelines about treating empty and null string equally): public static class Extensions { public static string GetDomain(this IIdentity identity) { string s = identity.Name; int stop = s.IndexOf(“\\”); return (stop > -1) ? s.Substring(0, stop) : string.Empty; } public static string … Read more

Difference between PrincipalSearcher and DirectorySearcher

I’ve spent a lot of time analyzing the differences between these two. Here’s what I’ve learned. DirectorySearcher comes from the System.DirectoryServices namespace. PrincipalSearcher comes from the System.DirectoryServices.AccountManagement namespace, which is built on top of System.DirectoryServices. PrincipalSearcher internally uses DirectorySearcher. The AccountManagement namespace (i.e. PrincipalSearcher) was designed to simplify management of User, Group, and Computer objects … Read more

System.DirectoryServices.DirectoryServicesCOMException: An operations error occurred

I had exactly the same error and fixed it by changing the site’s application pool to run under the Network Service. In IIS: Select your site’s application pool Select Advanced Settings on the right-hand side On the Advanced Settings pop-up window, scroll down to the Process Model group Change the first option called Identity to … Read more

How to get all the AD groups for a particular user?

You should use System.DirectoryServices.AccountManagement. It’s much easier. Here is a nice code project article giving you an overview on all the classes in this DLL. As you pointed out, your current approach doesn’t find out the primary group. Actually, it’s much worse than you thought. There are some more cases that it doesn’t work, like … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)