pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

使用圖形 api 以編程方式在 azure 活動目錄中注冊

Programmatically register app in azure active directory using graph api(使用圖形 api 以編程方式在 azure 活動目錄中注冊應用程序)
本文介紹了使用圖形 api 以編程方式在 azure 活動目錄中注冊應用程序的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在嘗試使用圖形 API 在 Azure AD 中注冊應用程序,我有一個方法 CallRestAPI 將發出請求.下面是代碼

I am trying to register an application in Azure AD using graph API, I have a method CallRestAPI which will make the request. Below is the code

    public async Task<Response> AzureADApp()
    {
        Response responseMessage = new Response();
        try
        {
            var token = GenerateToken();
            List<(string, string)> listHeaders = new List<(string, string)>();
            listHeaders.Add(("Authorization", string.Concat("Bearer" + " " + token)));
            listHeaders.Add(("Content-Type", "application/json"));

            List<(string, string)> param = new List<(string, string)>();
            param.Add(("displayName", "VS1Application123"));
            param.Add(("homepage", "https://localhost:44358/"));
            param.Add(("identifierUris", "https://G7CRM4L/6958490c-21ae-4885-804c-f03b3add87ad"));

            string callUrl = "https://graph.windows.net/G7CRM4L/applications/?api-version=1.6";
            var result = CallRestAPI(callUrl, "", Method.POST, listHeaders, param);

        }
        catch (Exception ex)
        {
            responseMessage.StatusCode = Convert.ToInt16(HttpStatusCode.InternalServerError);
        }
        return responseMessage;
    }

    public async Task<IRestResponse> CallRestAPI(string BaseAddress, string SubAddress, Method method, List<(string, string)> headersList = null, List<(string, string)> paramsList = null)
    {
        var call = new RestClient(BaseAddress + SubAddress);
        var request = new RestRequest(method);

        if (headersList != null)
        {
            foreach (var header in headersList)
            {
                request.AddHeader(header.Item1, header.Item2);
            }
        }
        if (paramsList != null)
        {
            foreach (var param in paramsList)
            {
                request.AddParameter(param.Item1, param.Item2);
            }
        }

        var response = call.ExecuteTaskAsync(request);

        return response.Result;
    }

我認為我在正文中發送參數的方式不正確,任何人都可以指導我如何使這段代碼工作,或者有更好的方法來實現同樣的效果嗎?謝謝你.

I think the way I am sending parameters in the body is not correct can anyone guide me how to make this code work or is there a better way to achieve the same? Thank you.

推薦答案

實現相同的更好方法,即使用 Azure AD 注冊應用程序將使用 Azure AD Graph 客戶端庫

A better way to achieve the same i.e. register an app with Azure AD will be to make use of Azure AD Graph Client Library

我說這是一種更好的方法,因為當您使用客戶端庫時,您可以獲得多種好處,例如無需處理原始 HTTP 請求、編寫更方便和聲明性的 C# 代碼,這取決于經過良好測試的庫、異步支持等.

I say it's a better approach because when you use the client library you reap multiple benefits like no raw HTTP request handling, writing more convenient and declarative C# code, depending on a well tested library, async support etc.

我想使用的底層 Graph API 仍然是一樣的

Underlying Graph API used will still be the same I suppose

POST https://graph.windows.net/{tenant-id}/applications?api-version=1.6

這里是創建 Azure AD 應用程序的示例代碼 (C#)

Here is sample code (C#) to create an Azure AD application

請注意,我將 app.PublicClient 標志保留為 true 以注冊為本機應用程序.如果要將其注冊為 Web 應用程序,可以將其設置為 false.

Notice that I've kept app.PublicClient flag as true to register as a native application. You can set it to false if you want to register it as a web application.

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;
        using Microsoft.Azure.ActiveDirectory.GraphClient;
        using Microsoft.IdentityModel.Clients.ActiveDirectory;

        namespace CreateAzureADApplication
        {
            class Program
            {
                static void Main(string[] args)
                {

                    ActiveDirectoryClient directoryClient;

                    ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(new Uri("https://graph.windows.net/{yourAADGUID}"),
            async () => await GetTokenForApplication());


                    Application app = new Application();
                    app.DisplayName = "My Azure AD Native App";
                    app.PublicClient = true;
                    app.Homepage = "https://myazureadnativeapp";
                    activeDirectoryClient.Applications.AddApplicationAsync(app).GetAwaiter().GetResult();

                 }

             public static async Task<string> GetTokenForApplication()
             {
                   AuthenticationContext authenticationContext = new AuthenticationContext(
                "https://login.microsoftonline.com/{yourAADGUID}",
                false);

            // Configuration for OAuth client credentials 

                ClientCredential clientCred = new ClientCredential("yourappclientId",
                    "yourappclientsecret"
                    );
                AuthenticationResult authenticationResult =
                    await authenticationContext.AcquireTokenAsync("https://graph.windows.net", clientCred);

                return authenticationResult.AccessToken;

            }
          }
        }

設置:我在 Azure AD 中注冊了一個應用程序,該應用程序具有應用程序權限所需的權限 - 讀取和寫入所有應用程序并為此應用程序授予權限.現在使用此應用程序的客戶端 ID 和客戶端密碼,獲取令牌并調用 Azure AD Graph API 來創建應用程序.使用應用程序權限不是強制性的,您也可以通過提示用戶輸入憑據來使用委派權限.查看更詳細示例的鏈接(舊示例但仍然有用).

Setup: I have an application registered in Azure AD, which has required permissions as application permission - Read and Write all applications and grant permissions is done for this app. Now using this application's client id and client secret, a token is acquired and Azure AD Graph API is called to create an application. It is not mandatory to use application permissions, you can also use delegated permissions by prompting user for credentials. See links to more detailed examples (old ones but still useful).

  • 使用 Graph 客戶端庫的控制臺應用程序

Web 應用使用 Graph 客戶端庫調用 Graph

Azure AD Graph 客戶端庫 2.0 公告頁面

附帶說明,您可以使用較新的 Microsoft Graph API 也一樣,

On a side note, you could do this using the newer Microsoft Graph API as well,

    POST https://graph.microsoft.com/beta/applications

但創建應用程序的能力仍處于測試階段,因此不推薦用于生產工作負載.因此,即使 Microsoft Graph API 會被推薦用于大多數場景,至少對于這個場景,使用 Azure AD Graph API 是當前的方式.

but the ability to create applications is still in beta and hence not recommeded for production workloads. So even though Microsoft Graph API would be recommende for most scenarios, at least for this one, using Azure AD Graph API is the way to go currently.

我在類似的 在這里發帖.

這篇關于使用圖形 api 以編程方式在 azure 活動目錄中注冊應用程序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

ASP.NET Core authenticating with Azure Active Directory and persisting custom Claims across requests(ASP.NET Core 使用 Azure Active Directory 進行身份驗證并跨請求保留自定義聲明)
ASP.NET Core 2.0 Web API Azure Ad v2 Token Authorization not working(ASP.NET Core 2.0 Web API Azure Ad v2 令牌授權不起作用)
How do I get Azure AD OAuth2 Access Token and Refresh token for Daemon or Server to C# ASP.NET Web API(如何獲取守護進程或服務器到 C# ASP.NET Web API 的 Azure AD OAuth2 訪問令牌和刷新令牌) - IT屋-程序員軟件開發技
.Net Core 2.0 - Get AAD access token to use with Microsoft Graph(.Net Core 2.0 - 獲取 AAD 訪問令牌以與 Microsoft Graph 一起使用)
Azure KeyVault Active Directory AcquireTokenAsync timeout when called asynchronously(異步調用時 Azure KeyVault Active Directory AcquireTokenAsync 超時)
Getting access token using email address and app password from oauth2/token(使用電子郵件地址和應用程序密碼從 oauth2/token 獲取訪問令牌)
主站蜘蛛池模板: 等离子空气净化器_医用空气消毒机_空气净化消毒机_中央家用新风系统厂家_利安达官网 | 泰安办公家具-泰安派格办公用品有限公司 | 长江船运_国内海运_内贸船运_大件海运|运输_船舶运输价格_钢材船运_内河运输_风电甲板船_游艇运输_航运货代电话_上海交航船运 | 电销卡_北京电销卡_包月电话卡-豪付网络 | 柔软云母板-硬质-水位计云母片组件-首页-武汉长丰云母绝缘材料有限公司 | 本安接线盒-本安电路用接线盒-本安分线盒-矿用电话接线盒-JHH生产厂家-宁波龙亿电子科技有限公司 | Eiafans.com_环评爱好者 环评网|环评论坛|环评报告公示网|竣工环保验收公示网|环保验收报告公示网|环保自主验收公示|环评公示网|环保公示网|注册环评工程师|环境影响评价|环评师|规划环评|环评报告|环评考试网|环评论坛 - Powered by Discuz! | elisa试剂盒价格-酶联免疫试剂盒-猪elisa试剂盒-上海恒远生物科技有限公司 | 扫地车厂家-山西洗地机-太原电动扫地车「大同朔州吕梁晋中忻州长治晋城洗地机」山西锦力环保科技有限公司 | 汽液过滤网厂家_安平县银锐丝网有限公司 | 3A别墅漆/3A环保漆_广东美涂士建材股份有限公司【官网】 | 流量检测仪-气密性检测装置-密封性试验仪-东莞市奥图自动化科技有限公司 | 断桥铝破碎机_铝合金破碎机_废铁金属破碎机-河南鑫世昌机械制造有限公司 | 鲁尔圆锥接头多功能测试仪-留置针测试仪-上海威夏环保科技有限公司 | 无线联网门锁|校园联网门锁|学校智能门锁|公租房智能门锁|保障房管理系统-KEENZY中科易安 | 中天寰创-内蒙古钢结构厂家|门式刚架|钢结构桁架|钢结构框架|包头钢结构煤棚 | 德国BOSCH电磁阀-德国HERION电磁阀-JOUCOMATIC电磁阀|乾拓百科 | 护腰带生产厂家_磁石_医用_热压护腰_登山护膝_背姿矫正带_保健护具_医疗护具-衡水港盛 | 广州办公室设计,办公室装修,写字楼设计,办公室装修公司_德科 | 游戏版号转让_游戏资质出售_游戏公司转让-【八九买卖网】 | 工控机,嵌入式主板,工业主板,arm主板,图像采集卡,poe网卡,朗锐智科 | 聚丙烯酰胺PAM-聚合氯化铝PAC-絮凝剂-河南博旭环保科技有限公司 巨野电机维修-水泵维修-巨野县飞宇机电维修有限公司 | 新疆散热器,新疆暖气片,新疆电锅炉,光耀暖通公司 | 雾度仪_雾度计_透光率雾度仪价格-三恩时(3nh)光电雾度仪厂家 | 广州番禺搬家公司_天河黄埔搬家公司_企业工厂搬迁_日式搬家_广州搬家公司_厚道搬迁搬家公司 | 搜活动房网—活动房_集装箱活动房_集成房屋_活动房屋 | 电缆接头-防爆电缆接头-格兰头-金属电缆接头-防爆填料函 | 中红外QCL激光器-其他连续-半导体连续激光器-筱晓光子 | 隐形纱窗|防护纱窗|金刚网防盗纱窗|韦柏纱窗|上海青木装潢制品有限公司|纱窗国标起草单位 | 济宁工业提升门|济宁电动防火门|济宁快速堆积门-济宁市统一电动门有限公司 | SMN-1/SMN-A ABB抽屉开关柜触头夹紧力检测仪-SMN-B/SMN-C-上海徐吉 | 编织人生 - 权威手工编织网站,编织爱好者学习毛衣编织的门户网站,织毛衣就上编织人生网-编织人生 | 泵阀展|阀门展|水泵展|流体机械展 -2025上海国际泵管阀展览会flowtech china | 在线浊度仪_悬浮物污泥浓度计_超声波泥位计_污泥界面仪_泥水界面仪-无锡蓝拓仪表科技有限公司 | 武汉高温老化房,恒温恒湿试验箱,冷热冲击试验箱-武汉安德信检测设备有限公司 | 快速卷帘门_硬质快速卷帘门-西朗门业| 捆扎机_气动捆扎机_钢带捆扎机-沈阳海鹞气动钢带捆扎机公司 | 杜甫仪器官网|实验室平行反应器|升降水浴锅|台式低温循环泵 | 无纺布包装机|径向缠绕包装机|缠绕膜打包机-上海晏陵智能设备有限公司 | 集装箱箱号识别_自重载重图像识别_铁路车号自动识别_OCR图像识别 | 品牌策划-品牌设计-济南之式传媒广告有限公司官网-提供品牌整合丨影视创意丨公关活动丨数字营销丨自媒体运营丨数字营销 |