将Active Directory成员列表传递到MVC视图

时间:2018-10-08 14:18:47

标签: c# model-view-controller active-directory

试图从Active Directory中获取属于某个组的列表成员以在mvc视图中显示。代码会运行,但是当模型中的用户数超过组中的用户数时,我只有一个用户在模型中显示。我知道我的退货是错误的,但是我不知道如何解决它以显示所有成员

模型类:

public class ADMemberDetails
{ 
    public string Username { get; set; }

    public string DisplayName { get; set; }

    public string SamAccountName { get; set; }
}

控制器

   public  ActionResult Index(string id, int? message, int? Remove, int ? error, int? adderror)
    {            
       ViewBag.Group= id;
           ADMemberDetails model = new ADMemberDetails();

           var domainContext = new PrincipalContext(ContextType.Domain);

            using (var user = GroupPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, id))
            {
            if (user != null)
            {

                foreach (var accountid in user.GetMembers())
                {

                    model.DisplayName = accountid.DisplayName;
                    model.SamAccountName = accountid.SamAccountName;
                    model.Username = accountid.Name;
                }
            }              
        }     

        return View(model);          
    }

查看        @model ADMemberDetails                                                                                                                             @ Html.Label(“显示名称”)                                                                               @ Html.Label(“ SamAccount”)                                                                               @ Html.Label(“名称”)                                              

            </thead>
            <tbody>


                @foreach (var output in Model)
                {
                <tr>

                    <th>

                        @Html.ActionLink("Remove", "RemoveUserFromGroup", new { userId = output.Username, groupName = @ViewBag.Group }, new { @class = "btn btn-xs btn-danger" })
                    </th>


                    <th>@output.DisplayName</th>
                    <th>@output.SamAccountName</th>
                    <th>@output.Username</th>

                </tr>
                }
            </tbody>

        </table>

0 个答案:

没有答案