zoukankan      html  css  js  c++  java
  • 无责任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>

    运行结果如图

    1

  • 相关阅读:
    vue+node.js+webpack开发微信公众号功能填坑——组件按需引入
    myeclipse打开jsp页面慢或者卡死
    myeclipse自动添加注释
    解决java.lang.NoSuchMethodError:org.joda.time.DateTime.withTimeAtStartOfDay() Lorg/joda/time/DateTime
    Echarts柱状图实现不同颜色渐变色
    《Python学习手册 第五版》 -第38章 被管理的属性
    《Python学习手册 第五版》 -第37章 Unicode和字节串
    《Python学习手册 第五版》 -第36章 异常的设计
    《Python学习手册 第五版》 -第35章 异常对象
    《Python学习手册 第五版》 -第34章 异常编写细节
  • 原文地址:https://www.cnblogs.com/shyleoking/p/4719020.html
Copyright © 2011-2022 走看看