2015-07-30 5 views
5

Я получаю 403 Forbidden ответ от Azure AD при попытке создания приложения с помощью Graph API:403 Forbidden от Azure Graph API

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 имеет все разрешения: Permissions in Azure AD

Что мне не хватает?

EDIT: Я зарегистрировал собственный клиент в 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()); 
     } 

ответ

5

Изменение каталога requires consent from an admin user. Таким образом, вам нужно будет получить токен доступа от пользователя, например. через OAuth, вместо токена для клиента.

Существует немало из samples at GitHub, которые показывают поток авторизации, например. https://github.com/AzureADSamples/WebApp-GraphAPI-DotNet.

+1

У вас его есть, добавьте рабочий код к вопросу. Благодаря! – Eric

2

Альтернативой может быть использование ActiveDirectoryClient из пакета NuGet Microsoft.Azure.ActiveDirectory.GraphClient.

private static async Task CreateApplication(string tenantId, string clientId, 
    string redirectUri) 
{ 
    var graphUri = new Uri("https://graph.windows.net"); 
    var serviceRoot = new Uri(graphUri, tenantId); 
    var activeDirectoryClient = new ActiveDirectoryClient(serviceRoot, 
     async() => AcquireTokenAsyncForUser("https://login.microsoftonline.com/" + tenantId, 
      clientId, redirectUri)); 

    var app = new Application 
     { 
      Homepage = "https://localhost", 
      DisplayName = "My Application", 
      LogoutUrl = "https://localhost", 
      IdentifierUris = new List<string> { "https://tenant.onmicrosoft.com/MyApp" }, 
      ReplyUrls = new List<string> { "https://localhost" } 
     }; 

    await activeDirectoryClient.Applications.AddApplicationAsync(app); 

    Console.WriteLine(app.ObjectId); 
} 

private static string AcquireTokenAsyncForUser(string authority, string clientId, 
    string redirectUri) 
{ 
    var authContext = new AuthenticationContext(authority, false); 
    var result = authContext.AcquireToken("https://graph.windows.net", 
     clientId, new Uri(redirectUri), PromptBehavior.Auto); 

    return result.AccessToken; 
} 
3

Добавление к @ MrBrink Ответим - вы должны убедиться, что лицо, добавив разрешения в Azure каталогов UI Активный фактически является администратором. Если у вас есть доступ к Azure Active Directory и не администратор, то WILL все еще позволяет назначать разрешения - однако они будут применяться только в области пользователя.

+0

Точно, делегированные разрешения, которые не требуют прав администратора, должны получить для пользователя «oauth2PermissionGrant», и никакие «appRoles» для разрешений приложений не будут созданы до тех пор, пока администратор не согласится. – juunas