安忍不动如大地,静虑深思如秘藏
雄鹰虽然有时候飞的比鸡还低 但鸡永远也不可能飞到鹰的高度

3.1 Index用户列表

或许当前域下的用户列表

[Authorize]
public async Task<ActionResult> Index()
{
    var userList = new List<IUser>();

    try
    {
        var client = AuthenticationHelper.GetActiveDirectoryClient();
        var pagedCollection = await client.Users.ExecuteAsync();
        while (pagedCollection != null)
        {
            userList.AddRange(pagedCollection.CurrentPage.ToList());
            pagedCollection = await pagedCollection.GetNextPageAsync();
        }
    }
    catch (Exception e)
    {
        if (e.Message == "Authorization Required.")
        {
            HttpContext.GetOwinContext().Authentication.Challenge(OpenIdConnectAuthenticationDefaults.AuthenticationType);
            return View(userList);
        }
    }
    return View(userList);
}

Index被修饰为[Authorize],当用户没有登录就会跳转到登录界面要求用户登录,当身份被验证后将执行Action的代码。

var client = AuthenticationHelper.GetActiveDirectoryClient();

上面这行代码获得了Microsoft.Azure.ActiveDirectory.GraphClient.ActiveDirectoryClient对象,该对象是对Azure AD Graph API的封装,该实例提供通过租户对象 ID和通过使用“Me”别名的两种Azure AD Graph REST API进行的服务。

Microsoft.Azure.ActiveDirectory.GraphClient.ActiveDirectoryClien实例暴露的属性,可以通过对Context和Query来了解到对Url的封装。

比如

l Applications中的BaseUri的值是https://graph.chinacloudapi.cn/<你的租户ID>

l Users的Query的Url是https://graph.chinacloudapi.cn/<你的租户ID>/users

l DeletedDirectoryObjectsde Query的Url是https://graph.chinacloudapi.cn/<你的租户ID>/deletedDirectoryObjects

有意思的是Me,Me是别名仅当使用 OAuth 授权代码授予类型(3 重)身份验证时,此别名才可用。此别名不区分大小写。它将替换 URL 中的对象 ID 或租户域。使用此别名时,Graph API 将从附加到请求的令牌中提供的声明获取用户。所以Me属性中提供的CreatedObjects、CreatedOnBehalfOf、DirectReports、Manager、MemberOf、Members、OwnedObjects、Owners这些操作的Url都是如下格式

https://graph.chinacloudapi.cn/<你的租户ID>/me/<操作名称>

所以说ActiveDirectoryClient提供的非常良好的RESTAPI的封装。

var pagedCollection = await client.Users.ExecuteAsync();

Users以IPagedCollection<IUser>对象返回,每Page包含一定数量的User,我们需要遍历集合中的User对象。

最后将我们获得的User集合返回给View。对应的View的代码如下

@model IEnumerable<Microsoft.Azure.ActiveDirectory.GraphClient.IUser>


@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>

<div class="table-responsive">
    <table id="directoryObjects" class="table table-bordered table-striped table-condensed">
        <tr>
            <th>
                用户名
            </th>
            <th>
                显示名称
            </th>
            <th>
                别名
            </th>
            <th>
                职务
            </th>
            <th />
        </tr>
        @foreach (var item in Model)
        {
            var user = item as Microsoft.Azure.ActiveDirectory.GraphClient.User;
            <tr>
                <td>
                    @Html.ActionLink(item.UserPrincipalName, "Details", new { objectId = item.ObjectId })
                </td>
                <td>
                    @Html.DisplayFor(modelItem => user.DisplayName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => user.MailNickname)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => user.JobTitle)
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { objectId = item.ObjectId }) <br />
                    @Html.ActionLink("Delete", "Delete", new { objectId = item.ObjectId })  <br />
                </td>
            </tr>
        }
    </table>
</div>

运行结果如图

1

posted on 2015-08-10 19:18  害羞的狮子王  阅读(464)  评论(0编辑  收藏  举报