开发者

Update Full Name in Active Directory

I've been thrust into the deep end with Active Directory (I'm a web developer,开发者_JAVA技巧 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

Update Full Name in Active Directory

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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜