English 中文(简体)
Update Full Name in Active Directory
原标题:

I ve been thrust into the deep end with Active Directory (I m a web developer, go fig) I have a user that I ve changed first/last name on but the Full Name hasn t changed which is causing issues with sharing off a BCM database. How do I refresh it so the Full Name is updated.

I have no idea how AD works but for some reason the higher ups have decided it s my job.

Any help would be really appreciated.

最佳回答

Are you on .NET 3.5 ? If so, go check out this MSDN article: Managing Directory Security Principals in the .NET Framework 3.5.

Check out this CodeProject article: Howto: (Almost) Everything In Active Directory via C#

Check out this list of Code Samples for System.DirectoryServices on MSDN.

If you re serious about Active Directory Programming in C# or VB.NET, go buy this book:

The .NET Developer s Guide to Directory Services Programming

alt text

Updating the "full name" (really: DisplayName) should be as easy as (on .NET 3.5):

PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "yourDomain");

UserPrincipal up = UserPrincipal.FindByIdentity(ctx, "(your user name)");

if (up != null)  // we found the user
{
   up.DisplayName = "new display name for your user";
   up.Save();
}

That s it! :-)

Mind you : you need to pass in the NetBIOS domain name (e.g. "MICROSOFT") - not a DNS-style name (microsoft.com) to the constructor of the PrincipalContext.

Hope this helps!

Marc

UPDATE for .NET 2.0:

Here s the code if you re running on .NET 2.0 and need to update the "displayName" for a user, given his "SAMAccountName" (his username without the domain, basically):

// set the root for the search
DirectoryEntry root = new DirectoryEntry("LDAP://dc=yourcompany,dc=com");

// searcher to find user in question
DirectorySearcher ds = new DirectorySearcher(root);

// set options
ds.SearchScope = SearchScope.Subtree;
ds.Filter = string.Format("(&(sAMAccountName={0})(objectCategory=Person))", yourUserName);

// do we find anyone by that name??
SearchResult result = ds.FindOne();

if (result != null)
{
    // if yes - retrieve the full DirectoryEntry for that user
    DirectoryEntry userEntry = result.GetDirectoryEntry();

    // set the "displayName" property to the new value
    userEntry.Properties["displayName"].Value = yourNewUserFullName;

    // save changes back to AD store
    userEntry.CommitChanges();
}
问题回答

暂无回答




相关问题
Using JavaScript to get an LDAP multi-valued string attribute

I am trying to retrieve an object attribute in Active Directory that appears to be a multi-valued string (See canonicalName). After performing a search: var conn; conn.Open = Provider=ADsDSOObject; ...

Test "User Must Change Password" field in .Net 3.5

I m trying to perform some basic AD User managment tasks in C# using .Net 3.5 I ve got a System.DirectoryServices.AccountManagement.UserPrincipal object that contains the user details. I can call ...

SSIS Script Task connecting to AD

I have written a SSIS 2005 script task that connects to Active Directory and reads user accountnames to store in database. I was able to successfully test this on my local system by executing dtexec....

Update Full Name in Active Directory

I ve been thrust into the deep end with Active Directory (I m a web developer, go fig) I have a user that I ve changed first/last name on but the Full Name hasn t changed which is causing issues with ...

Authenticate against Active Directory/ISA from php [closed]

I have a complicated problem, exacerbated by the fact I don t really know where to start! Over the last few years, I ve developed a number of php web-based systems. When I built them, our network was ...

热门标签