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>

运行结果如图

无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.1 Index用户列表]的更多相关文章

  1. 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息]

    三.使用Azure AD管理用户信息 在上一章我们采用OpenID的方案和Azure AD交互进行身份验证,本章节我们继续了解如何在Azure AD中创建用户,列出用户信息,修改用户信息和删除用户信息 ...

  2. 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.4 Edit修改用户信息]

    3.4 Edit修改用户信息 我们用FormCollection简化了表单提交,非常方便的进行用户信息修改. [HttpPost, Authorize] public async Task<Ac ...

  3. 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.3 Details用户详细信息]

    3.3 Details用户详细信息 用户详细信息是通过objectId获取.代码如下 public async Task<ActionResult> Details(string obje ...

  4. 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.2 Create创建用户]

    3.2 Create创建用户 [HttpPost, Authorize] public async Task<ActionResult> Create( [Bind(Include = & ...

  5. 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.5 Delete删除用户]

    3.5 Delete删除用户 删除也是通过ObjectID获得对象进行删除 [Authorize] public async Task<ActionResult> Delete(strin ...

  6. 无责任Windows Azure SDK .NET开发入门篇二[使用Azure AD 进行身份验证-2.2身份验证开发]

    2.2身份验证开发 在我们的案例中,我们是用户通过Web应用程序进行身份识别. 上面的图示说明了如下的一些概念 l Azure AD 是标识提供程序,负责对组织的目录中存在的用户和应用程序的标识进行验 ...

  7. 无责任Windows Azure SDK .NET开发入门篇二[使用Azure AD 进行身份验证]

    二.使用Azure AD进行身份验证 之所以将Azure AD 作为开始,是应为基本上我们所有应用都需要进行安全管理.Azure Active Directory (Azure AD) 通过以下方式简 ...

  8. 无责任Windows Azure SDK .NET开发入门篇二[使用Azure AD 进行身份验证--2.1使用Azure AD需要了解几个概念]

    2.1使用Azure AD需要了解几个概念 l Azure AD目录 当你注册 Microsoft 云服务时,便会获得一个 Azure AD 目录.你可根据需要创建更多的目录.例如,可以将第一个目录保 ...

  9. 无责任Windows Azure SDK .NET开发入门篇(一):开发前准备工作

    Windows Azure开发前准备工作 什么是 Azure SDK for .NET?微软官方告诉我们:Azure SDK for .NET 是一套应用程序,其中包括 Visual Studio 工 ...

随机推荐

  1. ecshop 更新首页flash样式

    未测试 ECSHOP默认的只有几种很普通的FLASH图片切换样式,想不想自己也换一种呢? 今天摸索了下,算是弄懂了,和大家分享下 首先在网上找到你想要的FLASH切换样式[google一下],把那个S ...

  2. 【英语】Bingo口语笔记(29) - Run系列

  3. CentOS 6安装mock

    最近工作中需要用到mock,这里介绍两种安装方式.本文的环境为CentOS 6.4 x86_64. 一,使用yum安装mock 安装第三方yum源RPMForge Centos5 64位 wget h ...

  4. 【转】A*寻路算法 C++实现

    头文件:AStarPathFinding #ifndef ASTARPATHFINDING_H #define ASTARPATHFINDING_H #include <queue>//为 ...

  5. .vdat文件怎么打开

    http://tieba.baidu.com/p/2947300642 无需转换,把后缀修改为MP4,就可以了

  6. ubuntu 切换工作区域

    在Ubuntu 13.04中,默认是不激活多桌面工作空间的,所以在之前的版本可以在启动器看到的那个像“田”字的“工作区切换器”图标没有了,始终只有一个桌面了.要激活工作空间,在system setti ...

  7. IPC_共享内存

    在IPC(InterProcess Communication)的通信模式下,不管是使用消息队列还是共享内存,甚至是信号量,每个IPC的对象(object)都有唯一的名字,称为“键”(key).通过“ ...

  8. SmartGit STUDY 2

    The Index The Index is an intermediate cache for preparing a commit. With SmartGit, you can make hea ...

  9. PHP开发常见问题解决列表

    1. 学习Zend Framework tutorial过程中的问题 (1)执行"zf create project zf-tutorial"出现如下错误: '"php. ...

  10. Delphi RxRichEdit高级操作

    unit InsertRichEditUnit;interfaceuses  Windows, Messages, SysUtils, Classes, Graphics, Controls, For ...