5

Graph API を使用してアプリケーションを作成しようとすると、Azure AD から 403 Forbidden 応答が返されます。

private static void CreateApplicationViaPost(string tenantId, string clientId, string clientSecret)
{
    var authContext = new AuthenticationContext(
        string.Format("https://login.windows.net/{0}",
        tenantId));

    ClientCredential clientCred = new ClientCredential(clientId, clientSecret);

    AuthenticationResult result = authContext.AcquireToken(
        "https://graph.windows.net",
        clientCred);

    HttpClient client = new HttpClient();
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);

    const string json = @"{ displayName: ""My test app"", logoutUrl: ""http://logout.net"", identifierUris: [ ""http://identifier1.com"" ], replyUrls: [ ""http://replyUrl.net"" ] }";
    HttpResponseMessage response = client.PostAsync(
        string.Format("https://graph.windows.net/{0}/applications?api-version=1.6", tenantId),
        new StringContent(json, Encoding.UTF8, "application/json")).Result;

    Console.WriteLine(response.ToString());
}

Azure AD に登録されているクライアントには、次のすべてのアクセス許可があります。 Azure AD のアクセス許可

私は何が欠けていますか?

編集: Azure AD にネイティブ クライアントを登録し、Windows Azure Active Directory への書き込み権限を付与しました。このコードは、Azure AD でアプリケーションを作成します。

private static void CreateApplicationViaPost(string tenantId, string clientId, string redirectUri)
        {
            var authContext = new AuthenticationContext(
                string.Format("https://login.windows.net/{0}",
                tenantId));

            AuthenticationResult result = authContext.AcquireToken("https://graph.windows.net", clientId, new Uri(redirectUri), PromptBehavior.Auto);

            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);

            const string json = @"{ displayName: ""My test app1"", homepage: ""http://homepage.com"", logoutUrl: ""http://logout1.net"", identifierUris: [ ""http://identifier11.com"" ], replyUrls: [ ""http://replyUrl1.net"" ] }";
            HttpResponseMessage response = client.PostAsync(
                string.Format("https://graph.windows.net/{0}/applications?api-version=1.6", tenantId),
                new StringContent(json, Encoding.UTF8, "application/json")).Result;

            Console.WriteLine(response.ToString());
        }
4

3 に答える 3

5

ディレクトリを変更するには、管理者ユーザーの同意が必要です。そのため、クライアントのトークンではなく、OAuth などを使用してユーザーからアクセス トークンを取得する必要があります。

https://github.com/AzureADSamples/WebApp-GraphAPI-DotNetなど、承認フローを示すサンプルが GitHubにかなりの数あります。

于 2015-07-31T06:49:15.750 に答える
3

@MrBrink の回答に追加 - Azure Active Directory UI でアクセス許可を追加する人が実際に管理者であることを確認する必要があります。Azure Active Directory へのアクセス権があり、管理者ではない場合でも、アクセス許可を割り当てることができますが、ユーザー スコープでのみ適用されます。

于 2015-12-28T21:52:54.887 に答える