.net 2.0中的System.DirectoryServices.AccountManagement

waq*_*med 4 .net asp.net directoryservices active-directory

有没有:

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

.net 2.0框架中的等价?它使用System.DirectoryServices.AccountManagement(ver 3.5)引用.我尝试在.net 2.0框架上使用该文件,但无济于事.

基本上,我想检索windows用户的完整用户名(名字和姓氏)(而不是Request.ServerVariables ["REMOTE_USER"],它只提供windows用户名)

mar*_*c_s 7

S.DS.AM命名空间是在.NET 3.5中引入的,不幸的是,它没有2.0版本.

您可以使用WindowsIdentity.GetCurrent()在ASP.NET应用程序中查询当前Windows用户.Name - 这将为您提供DOMAIN\UserName.

然后,您必须在AD中为具有DirectorySearcher对象的用户进行用户搜索,以便找到相应的DirectoryEntry.这将为您提供该用户的所有部分内容.

    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();
    }
Run Code Online (Sandbox Code Playgroud)

它有点复杂并且涉及.NET 2.0 - 无法改变:-(

  • 小心并确保丢弃目录条目.他们倾向于永远地闲逛,一旦你用完并发连接就会堵塞. (4认同)