我想創建一個新的操作方法,當它被調用時它將返回所有的Active Directory用戶名。 asp.net mvc應用程序和Active Directory位於同一個域中(並且當前在同一開發機器中)。如何使用PrincipalContext從Active Directory返回用戶詳細信息
所以我已經定義了以下操作方法: -
public ViewResult Details()
{
var c = repository.GetUserDetails3();
return View("Details2",c); }
及以下系統信息庫方法: -
public DirectoryEntry GetUserDetails3()
{
DirectoryEntry de = new DirectoryEntry();
using (var context = new PrincipalContext(ContextType.Domain, "WIN-SPDEV.com"))
{
using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))
{
foreach (var result in searcher.FindAll())
{
de = result.GetUnderlyingObject() as DirectoryEntry;
}
}
}
return de;
}
和下面的模型類: -
public class DirectoryUser
{public Nullable<Guid> Guid { get; set; }
public string Name { get; set; }
public string Username { get; set; }
}}
和我的觀點: -
@model IEnumerable<TMS.Models.DirectoryUser>
@{
ViewBag.Title = "Details2";
}
<h2>Details2</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th>
@Html.DisplayNameFor(model => model.Guid)
</th>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
<th>
@Html.DisplayNameFor(model => model.Username)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Guid)
</td>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Username)
</td>
,但是當我打電話的操作方法,我得到了以下錯誤: -
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type
The model item passed into the dictionary is of type 'System.DirectoryServices.DirectoryEntry', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[TMS.Models.DirectoryUser]'.
感謝您的答覆它工作得很好。但問題是,如果我檢索User.Identity.Name並將其分配給創建字段,則值將是諸如domainname \ user1之類的內容。而使用Principle類的時候沒有任何方法; UserPrincipal,Name,DisplayName將返回這樣的值。 Principle.userPrinciple會返回[email protected]。所以這會讓我的系統混淆? –
@johnG:爲什麼在您的系統中'CreatedBy = domain \ JohnG'會導致*混淆*?它完全記錄了**誰在你的系統中創建了這一段數據......不是'你想要什麼? –
是的。我正在使用Windows身份驗證的Intranet應用程序上工作。我有以下「objectname.CreatedBy = User.Identity.Name」。這將自動存儲爲域名\用戶名。在使用上面的原則類時,我會將用戶作爲「[email protected]」。因此,我將擁有表示同一用戶的不同字符串。所以我怎麼能克服這個衝突? –