簡體   English   中英

.net 2.0中的System.DirectoryServices.AccountManagement

[英]System.DirectoryServices.AccountManagement in .net 2.0

有沒有:

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用戶名)

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

它有點復雜並且涉及.NET 2.0 - 無法改變:-(

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM