久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用圖形 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 獲取訪問令牌)
主站蜘蛛池模板: 国产日韩欧美另类 | 成人福利网 | 亚洲男人网 | 成人午夜黄色 | 成人免费精品视频 | 午夜精品视频在线观看 | 九九久久这里只有精品 | 欧美精品网 | 老司机67194精品线观看 | 亚洲一区二区三区久久久 | 亚洲一区二区三区免费在线观看 | 日韩影院在线观看 | 国产分类视频 | 欧美午夜一区 | 亚洲网站在线播放 | 国产高清视频在线播放 | 国产激情在线观看视频 | 亚洲一区二区三区在线视频 | 国产精品高清一区二区三区 | 亚洲欧美激情视频 | 国产精品自拍视频网站 | 亚洲性在线| 久在线观看 | 中文字幕二区三区 | 亚欧精品一区 | 精品一区在线看 | 九九综合| 日本不卡一区 | 日韩成人在线视频 | 少妇午夜一级艳片欧美精品 | 国产a级毛片 | 国产原创视频 | 国产区久久 | 国产高清精品网站 | 国产一区二区三区 | 亚洲影音先锋 | 久久精品免费看 | 久久综合影院 | 超碰日韩| 91pao对白在线播放 | 欧日韩在线 |