2015-07-30 12 views
5

मैं जब ग्राफ़ एपीआई का उपयोग कर एक आवेदन बनाने का प्रयास कर Azure AD से एक 403 निषिद्ध प्रतिक्रिया मिल:403 Azure ग्राफ़ एपीआई से निषिद्ध

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

मुझे क्या याद आ रही है?

संपादित करें: मैं Azure AD में एक देशी ग्राहक पंजीकृत है और दे दी है यह विंडोज Azure सक्रिय निर्देशिका में लिखने के लिए अनुमतियाँ।

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 संशोधित करना: इस कोड Azure AD में एक आवेदन पैदा करते हैं। तो आपको किसी उपयोगकर्ता से एक्सेस टोकन प्राप्त करना होगा, उदा। क्लाइंट के लिए टोकन की बजाय ओएथ के माध्यम से।

samples at GitHub में से कुछ हैं जो प्राधिकरण प्रवाह दिखाते हैं, उदा। https://github.com/AzureADSamples/WebApp-GraphAPI-DotNet

+1

समझ गया, प्रश्न के लिए काम कोड जोड़ा गया। धन्यवाद! – Eric

2

Microsoft.Azure.ActiveDirectory.GraphClient NuGet पैकेज से ActiveDirectoryClient का उपयोग करने का एक विकल्प होगा।

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 सक्रिय निर्देशिका यूआई में अनुमतियाँ जोड़ने व्यक्ति वास्तव में एक व्यवस्थापक है बनाने की जरूरत है। यदि आपके पास Azure Active Directory तक पहुंच है और यह व्यवस्थापक नहीं है तो यह होगा अभी भी अनुमतियां असाइन करने दें - हालांकि वे केवल उपयोगकर्ता के दायरे पर ही लागू होंगे।

+0

बिल्कुल, प्रतिनिधि अनुमतियों को जिनके लिए व्यवस्थापक अधिकारों की आवश्यकता नहीं है, उन्हें उपयोगकर्ता के लिए 'oauth2PermissionGrant' प्राप्त करना चाहिए, और ऐप अनुमतियों के लिए' appRoles' नहीं बनाया जाएगा जब तक कोई व्यवस्थापक सहमति के माध्यम से नहीं जाता है। – juunas

संबंधित मुद्दे