2009-05-29 57 views
4

是否有:System.DirectoryServices.AccountManagement在.NET 2.0

字符串名称= System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName;

等价于.NET 2.0框架? 它使用System.DirectoryServices.AccountManagement(版本3.5)参考。我试图在.net 2.0框架中使用该文件,但无济于事。

基本上,我想要检索的Windows用户的用户全名(名和姓)(未Request.ServerVariables [“REMOTE_USER”]只给Windows用户名)

回答

7

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

您可以查询使用WindowsIdentity.GetCurrent当前的Windows用户在一个ASP.NET应用程序()名称 - 这给你域\用户名。

然后,你必须做一个用户搜索在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(); 
    } 

这是一个有点复杂,涉及.NET 2.0 - 不能改变这种:-(

马克

+0

市建局天才......它的工作完美千恩万谢 – waqasahmed 2009-05-29 10:33:20