本段代码是个通用性很强的sample code,不仅能够操作AAD本身,也能通过Azure Service Principal的授权来访问和控制Azure的订阅资源。(Azure某种程度上能看成是两个层级:AAD+Subscription)

下文中的代码是演示的screenshot中的红字2的部分。红字1的部分的permission实质上是赋予AAD service principal操作订阅的权限(这个需要切换var resource = “https://management.core.chinacloudapi.cn/“)

预先准备

  1. 注册一个Azure AD application
  2. 对这个aad application赋予适当的权限

sample code 如下:

 using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks; namespace AadGraphApi
{
class Program
{
static void Main(string[] args)
{
//Demo below AAD graph api
//1. List All users in AAD
//2. Check user existence
//3. Get AppRoleAssignment
//4. implement the appRoleAssignment //Test MoonCake Azure
//Task task = CnTest(); //Test Global Azure
Task task = CnTest();
var x = task;
Console.WriteLine("**--------done-------**");
Console.ReadLine();
}
// using Http Request to get Token
private static async Task<string> CnAppAuthenticationAsync()
{
// Using in Mooncake Azure
// Constants
var tenant = "";
var resource = "https://graph.chinacloudapi.cn";
//var resource = "https://management.core.chinacloudapi.cn/";
var clientID = "";
var secret = "";
// Ceremony
var authority = $"https://login.chinacloudapi.cn/{tenant}";
var authContext = new AuthenticationContext(authority);
var credentials = new ClientCredential(clientID, secret);
var authResult = await authContext.AcquireTokenAsync(resource, credentials);
return authResult.AccessToken;
} private static async Task CnTest()
{
var token = await CnAppAuthenticationAsync(); using (var client = new HttpClient())
{
//
//be careful for the specific parameters in the URI . replace it with yours
//
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); var apiUriUserExist = new Uri("https://graph.chinacloudapi.cn/{yourtenantid}/users/**.partner.onmschina.cn?api-version=1.6");
var apiUriListAllUser = new Uri("https://graph.chinacloudapi.cn/**.partner.onmschina.cn/users?api-version=1.6");
var apiUriGetAppRoleAssignment = new Uri("https://graph.chinacloudapi.cn/**。partner.onmschina.cn/users/**.partner.onmschina.cn/appRoleAssignments?api-version=1.6"); //var userExist = await DoesUserExistsAsync(client, apiUriUserExist);
//Console.WriteLine($"Does user exists? {userExist}"); var userLists = await ListAllUsers(client, apiUriListAllUser);
Console.WriteLine(userLists);
/*
var appRoleList = await GetAppRoleAssignment(client, apiUriGetAppRoleAssignment);
Console.WriteLine(appRoleList); //post request for AAD appRoleAssignment
await CnPostAppRoleAssignment(client);
//
*/
}
} private static async Task<bool> DoesUserExistsAsync(HttpClient client, Uri apiUri)
{
try
{
var payload = await client.GetStringAsync(apiUri);
return true;
}
catch (HttpRequestException)
{
return false;
}
} private static async Task<string> ListAllUsers(HttpClient client, Uri apiUri)
{
try
{
var payload = await client.GetStringAsync(apiUri);
return payload;
}
catch (HttpRequestException ex)
{
return ex.ToString();
}
}
}
}

本段代码通过授权去拿Azure AD 中的user。还有很多其他的操作,比如delete user, list all user , Azure提供了一系列的Graph API
同理我们也能通过Managment授权发送操作资源的http请求达到代码控制Azure订阅资源的目的。

AAD Service Principal获取azure user list (Microsoft Graph API)的更多相关文章

  1. 【Azure Developer】使用Microsoft Graph API 批量创建用户,先后遇见的三个错误及解决办法

    问题描述 在先前的一篇博文中,介绍了如何使用Microsoft Graph API来创建Azure AD用户(博文参考:[Azure Developer]使用Microsoft Graph API 如 ...

  2. 【Azure Developer】使用Microsoft Graph API 如何批量创建用户,用户属性中需要包含自定义字段(如:Store_code,Store_name等)

    Microsoft Graph 是 Microsoft 365 中通往数据和智能的网关. 它提供统一的可编程模型,可用于访问 Microsoft 365.Windows 10 和企业移动性 + 安全性 ...

  3. Microsoft Graph API -----起题 Graph API

    最近因为工作需要,接触学习使用了Microsoft Graph API.在看完Microsoft的Graph官方文档之后,也做了一些简单的案例,在Stack Overflow上做过一些回答.整体来说, ...

  4. 【Azure Developer】Python 获取Micrisoft Graph API资源的Access Token, 并调用Microsoft Graph API servicePrincipals接口获取应用ID

    问题描述 在Azure开发中,我们时常面临获取Authorization问题,需要使用代码获取到Access Token后,在调用对应的API,如servicePrincipals接口. 如果是直接调 ...

  5. 手把手:使用service principal连接Azure Media Service

    在简书中查看,请点击我. 关于相关内容解释,请参考docs文档 https://docs.microsoft.com/en-us/azure/media-services/previous/media ...

  6. 【Azure Developer】使用Microsoft Graph API创建用户时候遇见“401 : Unauthorized”“403 : Forbidden”

    问题描述 编写Java代码调用Mircrosoft Graph API创建用户时,分别遇见了"401 : Unauthorized"和"403 : Forbidden&q ...

  7. Azure登陆的两种常见方式(user 和 service principal登陆)

    通过Powershell 登陆Azure(Azure MoonCake为例)一般常见的有两种方式 1. 用户交互式登陆 前提条件:有一个AAD account 此种登陆方式会弹出一个登陆框,让你输入一 ...

  8. Azure App object和Service Principal

    为了把Identity(身份)和Access Management function(访问管理功能)委派给Azure AD,必须向Azure AD tenant注册应用程序.使用Azure AD注册应 ...

  9. 解决使用Microsoft Graph OAuth获取令牌时,没有refresh_token的问题

    今天在使用Microsoft Graph 的时候,发现按照官方文档,无论如何都不能获取refresh_token,其他都没问题,经过查询,发现是因为在第一步,获取code授权时,没有给离线权限(off ...

随机推荐

  1. asp.net core使用jexus部署在linux无法正确 获取远程ip的解决办法

    asp.net core程序部署在centos7(下面的解决方案,其他系统都能使用,这里只是我自己部署在centos7),使用服务器jexus进行部署,AppHost模式. 因为请求是由jexus进行 ...

  2. centos7.2部署vnc服务记录

    不做过多介绍了,下面直接记录下centos7系统下安装配置vncserver的操作记录 0)更改为启动桌面或命令行模式 获取当前系统启动模式 [root@localhost ~]# systemctl ...

  3. PHP从入门到精通(一)

    (一)PHP简介和基本知识 PHP(外文名:PHP: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言.语法吸收了C语言.Java和Perl的特点,利于 ...

  4. vue全局 关键字搜索 v-search

    一款基于 vuejs & weui 的全屏搜索组件:https://www.npmjs.com/package/vue-search

  5. E. Binary Numbers AND Sum

    链接 [http://codeforces.com/contest/1066/problem/E] 题意 给你长度分别为n,m的二进制串,当b>0时,对a,b,&运算,然后b右移一位,把 ...

  6. Linux内核第三节 20135332武西垚

    总结部分: Linux内核源代码: Arch 支持不同cpu的源代码:主要关注x86 Init   内核启动的相关代码:主要关注main.c,整个Linux内核启动代码start_kernel函数 K ...

  7. Python学习笔记(三)——条件语句、循环语句

    注:需注意代码的缩进格式 注:需注意代码的缩进格式 注:需注意代码的缩进格式 Python 与其他语言最大的区别就是,Python 的代码块不使用大括号 {} 来控制类,函数以及其他逻辑判断.pyth ...

  8. 我的software

    每个学计算机软件的同学都有可能经历以下的情况: 1.  哎,我家电脑开不了机了,来帮帮忙 2.  我耳机坏了,你给修修吧 3.  你能换手机屏不 4.  过来看下,我的Word打不开了 等等等等 这些 ...

  9. Maven的课堂笔记4

    9.Maven与MyEclipse2014结合 MyEclipse10以上的版本,对Maven支持的就比较好 9.2 Myeclipse配置 本地文件夹的C盘的.m2文件夹下必须得有这个setting ...

  10. PHP使用MySQL实现消息队列

    消息队列常用在流量削峰(秒杀场景),异步通信等地方. 大体的结构如下: 类似于消费者和生产者的关系,首先生产者在消息队列未满的时候,才将生产的产品放进消息队列中:消费者在消息队列不为空的时候,才从消息 ...