Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用图形api在azure active directory中以编程方式注册应用程序_C#_Azure Active Directory_Microsoft Graph Api_Azure Ad Graph Api - Fatal编程技术网

C# 使用图形api在azure active directory中以编程方式注册应用程序

C# 使用图形api在azure active directory中以编程方式注册应用程序,c#,azure-active-directory,microsoft-graph-api,azure-ad-graph-api,C#,Azure Active Directory,Microsoft Graph Api,Azure Ad Graph Api,我正在尝试使用graph API在Azure AD中注册一个应用程序,我有一个方法CallRestAPI,它将发出请求。 下面是代码 public async Task<Response> AzureADApp() { Response responseMessage = new Response(); try { var token = GenerateToken();

我正在尝试使用graph API在Azure AD中注册一个应用程序,我有一个方法
CallRestAPI
,它将发出请求。 下面是代码

    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;
    }
public异步任务AzureADApp()
{
Response responseMessage=新响应();
尝试
{
var token=GenerateToken();
List listHeaders=新列表();
Add((“Authorization”,string.Concat(“Bearer”+“”+token));
Add((“内容类型”、“应用程序/json”);
列表参数=新列表();
参数Add((“displayName”、“VS1Application123”);
参数添加((“主页”)https://localhost:44358/"));
参数添加((“标识栏“,”https://G7CRM4L/6958490c-21ae-4885-804c-f03b3add87ad"));
字符串CALULRL=”https://graph.windows.net/G7CRM4L/applications/?api-版本=1.6”;
var result=CallRestAPI(callUrl,“,Method.POST,listHeaders,param);
}
捕获(例外情况除外)
{
responseMessage.StatusCode=Convert.ToInt16(HttpStatusCode.InternalServerError);
}
返回响应消息;
}
公共异步任务CallRestAPI(字符串基地址、字符串子地址、方法、列表头列表=null、列表参数列表=null)
{
var call=new RestClient(BaseAddress+SubAddress);
var请求=新的重新请求(方法);
if(headersList!=null)
{
foreach(表头列表中的var表头)
{
request.AddHeader(header.Item1,header.Item2);
}
}
if(paramsList!=null)
{
foreach(参数列表中的变量param)
{
request.AddParameter(param.Item1,param.Item2);
}
}
var response=call.ExecuteTaskAsync(请求);
返回响应。结果;
}
我认为我在正文中发送参数的方式是不正确的。有人能指导我如何使这段代码工作吗?或者有更好的方法来实现这一点吗?
多谢各位

实现这一目标的更好方法是使用

我说这是一种更好的方法,因为当您使用客户机库时,您可以获得多种好处,例如无需原始HTTP请求处理、编写更方便的声明性C#代码,这取决于经过良好测试的库、异步支持等

我想,使用的底层图形API仍然是一样的

POST https://graph.windows.net/{tenant-id}/applications?api-version=1.6
下面是创建Azure广告应用程序的示例代码(C#)

请注意,我将app.PublicClient标志保持为true,以便注册为本机应用程序。如果要将其注册为web应用程序,可以将其设置为false

        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;

            }
          }
        }
但创建应用程序的能力仍处于测试阶段,因此不推荐用于生产工作负载。因此,尽管Microsoft Graph API将被推荐用于大多数场景,至少在这一场景中,使用Azure AD Graph API是当前的发展方向


我在这里的一篇类似文章中更详细地介绍了这一点。

感谢Rohit,我在.net core中运行了上述代码,我得到了一个异常“此平台不支持安全二进制序列化”。
    POST https://graph.microsoft.com/beta/applications