惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
N
Netflix TechBlog - Medium
宝玉的分享
宝玉的分享
Google DeepMind News
Google DeepMind News
腾讯CDC
G
Google Developers Blog
Martin Fowler
Martin Fowler
Microsoft Security Blog
Microsoft Security Blog
Recent Announcements
Recent Announcements
爱范儿
爱范儿
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
A
About on SuperTechFans
aimingoo的专栏
aimingoo的专栏
有赞技术团队
有赞技术团队
Jina AI
Jina AI
人人都是产品经理
人人都是产品经理
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
M
MIT News - Artificial intelligence
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
美团技术团队
WordPress大学
WordPress大学
阮一峰的网络日志
阮一峰的网络日志

博客园 - 害羞的狮子王

无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.5 Delete删除用户] 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.4 Edit修改用户信息] 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.3 Details用户详细信息] 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.2 Create创建用户] 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息] 无责任Windows Azure SDK .NET开发入门篇二[使用Azure AD 进行身份验证-2.2身份验证开发] 无责任Windows Azure SDK .NET开发入门篇二[使用Azure AD 进行身份验证--2.1使用Azure AD需要了解几个概念] 无责任Windows Azure SDK .NET开发入门篇二[使用Azure AD 进行身份验证] 无责任Windows Azure SDK .NET开发入门篇一[Windows Azure开发前准备工作] 建立小型开发团队的工作协作:翻译工作 建立小型开发团队的工作协作:讨论区 建立小型开发团队的工作协作:BugList 建立小型开发团队的工作协作:任务日历 建立小型开发团队的工作协作:简单的文档管理 建立小型开发团队的工作协作:管理调查和投票 建立小型开发团队的工作协作:管理链接 建立小型开发团队的工作协作:总览效果图 感谢微软BPOS4China技术支持组 远程为服务器安装Windows 2008 Server
无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管...
害羞的狮子王 · 2015-08-10 · via 博客园 - 害羞的狮子王

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