无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.1 Index用户列表]
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用户列表]的更多相关文章
- 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息]
三.使用Azure AD管理用户信息 在上一章我们采用OpenID的方案和Azure AD交互进行身份验证,本章节我们继续了解如何在Azure AD中创建用户,列出用户信息,修改用户信息和删除用户信息 ...
- 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.4 Edit修改用户信息]
3.4 Edit修改用户信息 我们用FormCollection简化了表单提交,非常方便的进行用户信息修改. [HttpPost, Authorize] public async Task<Ac ...
- 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.3 Details用户详细信息]
3.3 Details用户详细信息 用户详细信息是通过objectId获取.代码如下 public async Task<ActionResult> Details(string obje ...
- 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.2 Create创建用户]
3.2 Create创建用户 [HttpPost, Authorize] public async Task<ActionResult> Create( [Bind(Include = & ...
- 无责任Windows Azure SDK .NET开发入门篇三[使用Azure AD 管理用户信息--3.5 Delete删除用户]
3.5 Delete删除用户 删除也是通过ObjectID获得对象进行删除 [Authorize] public async Task<ActionResult> Delete(strin ...
- 无责任Windows Azure SDK .NET开发入门篇二[使用Azure AD 进行身份验证-2.2身份验证开发]
2.2身份验证开发 在我们的案例中,我们是用户通过Web应用程序进行身份识别. 上面的图示说明了如下的一些概念 l Azure AD 是标识提供程序,负责对组织的目录中存在的用户和应用程序的标识进行验 ...
- 无责任Windows Azure SDK .NET开发入门篇二[使用Azure AD 进行身份验证]
二.使用Azure AD进行身份验证 之所以将Azure AD 作为开始,是应为基本上我们所有应用都需要进行安全管理.Azure Active Directory (Azure AD) 通过以下方式简 ...
- 无责任Windows Azure SDK .NET开发入门篇二[使用Azure AD 进行身份验证--2.1使用Azure AD需要了解几个概念]
2.1使用Azure AD需要了解几个概念 l Azure AD目录 当你注册 Microsoft 云服务时,便会获得一个 Azure AD 目录.你可根据需要创建更多的目录.例如,可以将第一个目录保 ...
- 无责任Windows Azure SDK .NET开发入门篇(一):开发前准备工作
Windows Azure开发前准备工作 什么是 Azure SDK for .NET?微软官方告诉我们:Azure SDK for .NET 是一套应用程序,其中包括 Visual Studio 工 ...
随机推荐
- 深入理解ob_flush/flush
ob_flush/flush在手册中的描述, 都是刷新输出缓冲区, 并且还需要配套使用, 所以会导致很多人迷惑… 其实, 他们俩的操作对象不同, 有些情况下, flush根本不做什么事情.. ob_* ...
- Edit Control的Enter响应函数
Edit Control的Enter响应函数 在dialog中添加edit control ,选择“Multi_Line mode” MFC Class Wizard中添加Virtual Fu ...
- hql查询语句 内存中的情况,fetch迫切查询关键字
Classes.java package cn.itcast.hiberate.sh.domain; import java.io.Serializable; import java.util.Set ...
- WINHEX 使用教程
Winhex有完善的分区管理功能和文件管理功能,能自动分析分区链和文件簇链,能对硬盘进行不同方式不同程度的备份,甚至克隆整个硬盘:它能够编 辑任何一种文件类型的二进制内容(用十六进制显示)其磁盘编辑器 ...
- 什么是CC攻击,如何防止网站被CC攻击的方法总汇
CC攻击(Challenge Collapsar)是DDOS(分布式拒绝服务)的一种,也是一种常见的网站攻击方法,攻击者通过代理服务器或者肉鸡向向受害主机不停地发大量数据包,造成对方服务器资源耗尽,一 ...
- html --- canvas --- javascript --- 绘图方法
Canvas元素是HTML5的一部分,允许脚本语言动态渲染位图像. 如有疑问请访问链接:http://javascript.ruanyifeng.com/htmlapi/canvas.html < ...
- 关于C++虚函数的一些东西
先上概念,C++的多态性:系统在运行时根据对象类型,来确定调用哪个重载的成员函数的能力. 多态性是通过虚函数实现的.成员函数之前加了virtual,即成为虚函数. 有虚成员函数的类,编译器在其每个对象 ...
- 动态创建二维vector数组 C和C++ 及指针与引用的区别
二维vectorvector<vector <int> > ivec(m ,vector<int>(n)); //m*n的二维vector 动态创建m*n的二 ...
- 判断浏览器是IE的几种方式
<script> if(!+[1,])alert("这是ie浏览器"); else alert("这不是ie浏览器"); </script&g ...
- 僵尸进程&孤儿进程
http://www.cnblogs.com/Anker/p/3271773.html