我正在構建一個Intranet應用程序,並且正在使用Windows身份驗證。我有這樣的設置和工作,我通過@ Context.User.Identity.Name.net mvc與Active Directory集成並使用數據模型訪問System.DirectoryServices.AccountManagement
我能看到當前用戶的用戶名,我想查詢Active Directory來檢索顯示名稱,電子郵件地址等
我已經看過System.DirectoryServices.AccountManagement並從此創建了一個基本示例。我不清楚如何在整個應用程序中顯示這些信息。
我至今如下:
public class searchAD
{
// Establish Domain Context
PrincipalContext domainContext = new PrincipalContext(ContextType.Domain);
public searchAD()
{
// find the user
UserPrincipal user = UserPrincipal.FindByIdentity(domainContext, HttpContext.Current.User.Identity.Name);
if (user != null)
{
var userGuid = user.Guid;
var DisplayName = user.DisplayName;
var GivenName = user.GivenName;
var EmailAddress = user.EmailAddress;
}
}
}
我假設我需要創建一個視圖模型像這樣?
public class domainContext
{
public Nullable<Guid> userGuid { get; set; }
public string DisplayName { get; set; }
public string GivenName { get; set; }
public string EmailAddress { get; set; }
}
我希望能夠在應用程序的各個部分訪問這些信息。
我沒有收到任何錯誤,只是似乎錯過了將所有這些聯繫在一起的東西。
我試過你以前的代碼,不幸的是沒有工作。你能否澄清如何將DisplayName,GivenName等傳遞給我的View? – Stephen
修改爲更詳細的答案。 – bittenbytailfly
非常感謝。我想我快到了。只是在HttpContext上出錯 - 非靜態字段需要對象引用... – Stephen