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. tcpdump tutorial

    tcpdump tutorial */--> UP | HOME tcpdump tutorial Table of Contents 1 Options 2 Basic Usage 3 Com ...

  2. Android数据库一些源码分析

    对于批量数据插入这种最常见的情况来说,我们来看两种实现方式(两种都用了事务). 下面这种应该是最多人使用的插入数据的方法: public long addByExec(List<Person&g ...

  3. myeclipse svn配置

    在MyEclipse 9.0中安装SVN插件遇到一些问题,参考网上一些方法,最终解决.以下是个人认为比较简易的方法,供参考: 安装过程: (1)svn的插件版本site-1.8.14.zip(可根据自 ...

  4. Sde表结构分析

    原文 Sde表结构分析 今天开始想分析一下sde的表结构,希望能够弄明白sde一个要素类的每个Feature是如何存储的. 弄ArcSDE的人都知道,ArcSDE内一个要素类在关系数据库(以MS SQ ...

  5. 带删除小图标的EditText

    import android.content.Context; import android.graphics.Rect; import android.graphics.drawable.Drawa ...

  6. 【LeetCode 238】Product of Array Except Self

    Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...

  7. 判断图片加载完成,自适应iframe高度

    在做RSS订阅的时候遇到这样一个问题:点击文章标题时,弹出文章的详细界面.本来打算直接用弹出div层来显示文章的内容,但是设置div的overflow:scroll滚动条不好看,还有就是在androi ...

  8. 【LeetCode】226 - Invert Binary Tree

    Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9          to 4 / \ 7 2 / \ / \ 9 6 3 1   Notice: Goog ...

  9. python中的多线程【转】

    转载自: http://c4fun.cn/blog/2014/05/06/python-threading/ python中关于多线程的操作可以使用thread和threading模块来实现,其中th ...

  10. bzoj 3757 苹果树(树上莫队算法)

    [题意] 有若干个询问,询问路径u,v上的颜色总数,另外有要求a,b,意为将a颜色看作b颜色. [思路] vfk真是神系列233. Quote: 用S(v, u)代表 v到u的路径上的结点的集合. 用 ...