本段代码是个通用性很强的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. LOJ #6074. 「2017 山东一轮集训 Day6」子序列

    #6074. 「2017 山东一轮集训 Day6」子序列 链接 分析: 首先设f[i][j]为到第i个点,结尾字符是j的方案数,这个j一定是从i往前走,第一个出现的j,因为这个j可以代替掉前面所有j. ...

  2. [JDBC]ORA-01000: 超出打开游标的最大数(ORA-01000: maximum open cursors exceeded)

    问题产生的原因: Java代码在执行conn.createStatement()和conn.prepareStatement()的时候,相当于在数据库中打开了一个cursor.由于oracle对打开的 ...

  3. Oracle_忘记密码

    1.运行到C盘根目录 2.输入:SET ORACLE_SID = 你的SID名称 3.输入:sqlplus/nolog 4.输入:connect/as sysdba 5.输入:altre user s ...

  4. StackOverflow 问题

    StackOverflow  这个问题一般是你的程序里头可能是有死循环或递归调用所产生的:可以查看一下你的程序,也可以增大你JVM的内存~~~在Eclipse中JDK的配置中加上   -XX:MaxD ...

  5. SQL中not in 和not exists

    在SQL中倒是经常会用到子查询,而说到子查询,一般用的是in而不是exists,先不谈效率问题,就先说说会遇到哪些问题. 用到in当取反的时候,肯定先想到的就是not in.但是在使用not in的时 ...

  6. 关于第二次阅读作业中"银弹"“大泥球”等的个人理解

    这几天时间比较充裕,就一点一点的借助英语翻译(毕竟英语不好)阅读了一下老师建议的论文作品.感觉他们的思维和我们的是不在一个角度上的,在我们看来,编写代码的任务仅仅就是实现了设计文档中的功能,而这些在课 ...

  7. Linux内核分析第四章读书笔记

    第四章 进程调度 进程调度程序:确保进程能有效工作的一个内核子程序 决定将哪个进程投入运行,何时运行已经运行多长时间 进程调度程序可看做在可运行态进程之间分配有限的处理器时间资源的内核子系统 原则:只 ...

  8. Linux内核第八节 20135332武西垚

    第一种分类: I/O-bound:频繁进行I/O,并且需要花费很多时间等待I/O完成 CPU-bound:计算密集,需要大量的CPU时间进行运算 第二种分类: 批处理进程:不必与用户交互,常在后台进行 ...

  9. Github上传更新

    通过2天的时间,不停的网上找各种资料,今天下午终于可以登录上github for Windows 客户端了,,, 然后通过一整晚的摸索,也把项目上传到github里. github地址:https:/ ...

  10. 20150401 作业2 结对 四则运算ver 1.0

    Web項目下 Tomcat服務器的路徑 /WebContant/ 目錄下 SE2_2.jsp <%@ page language="java" contentType=&qu ...