System.DirectoryServices.AccountManagement in .net 2.0

Whether there is a:

string name = System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName;

equivalence in .net 2.0 framework? It uses the link System.DirectoryServices.AccountManagement (ver. 3.5). I tried to use this file in a .net 2.0 environment, but to no avail.

Basically, I want to get the full username (first and last name) of the Windows user (not Request.ServerVariables ["REMOTE_USER"], which gives only the Windows username)

+4
source share
1 answer

The S.DS.AM namespace was introduced in .NET 3.5 and, unfortunately, there is no version 2.0.

You can query the current Windows user in an ASP.NET application using WindowsIdentity.GetCurrent (). Name - this gives you DOMAIN \ UserName.

You will then need to search the user in AD for that user with a DirectorySearcher object to find the appropriate DirectoryEntry. This will give you all the pieces of this user.

string currentUser = WindowsIdentity.GetCurrent().Name; string[] domainUserName = currentUser.Split('\\'); string justUserName = domainUserName[1]; DirectoryEntry searchRoot = new DirectoryEntry("LDAP://dc=(yourcompany),dc=com"); DirectorySearcher ds = new DirectorySearcher(searchRoot); ds.SearchScope = SearchScope.Subtree; ds.PropertiesToLoad.Add("sn"); ds.PropertiesToLoad.Add("givenName"); ds.Filter = string.Format("(&(objectCategory=person)(samAccountName={0}))", justUserName); SearchResult sr = ds.FindOne(); if (sr != null) { string firstName = sr.Properties["givenName"][0].ToString(); string lastName = sr.Properties["sn"][0].ToString(); } 

This is a bit complicated and related to .NET 2.0 - cannot change this: - (

Mark

+7
source

All Articles