2016-01-01 7 views
6

मैं बिलिंग उपयोग और दरकार्ड विवरण लाने के लिए Azure REST API का उपयोग कर रहा हूं। का उपयोग करके टोकन प्राप्त करने के लिए AcquireToken() विधि, शुरुआत में मैंने केवल क्लाइंट आईडी का उपयोग किया जो तब लॉगिन विंडो में उपयोगकर्ता प्रमाण-पत्रों के लिए पूछता है।Azure बिलिंग उपयोग API 401 अनधिकृत

हालांकि, मैं गैर इंटरएक्टिव दृष्टिकोण रहा हूँ, इसलिए मैं ग्राहक साख जिसमें मैं ग्राहक आईडी और क्लाइंट सीक्रेट कुंजी पारित कर दिया करते थे।

लेकिन यह

जब मैं त्रुटि में गहराई से देखो, मैंने पाया कि यह त्रुटि

"पहुँच टोकन गलत दर्शकों या संसाधन से है" देता है "दूरस्थ सर्वर 401 अनधिकृत एक त्रुटि देता है" देता है

कृपया मुझे कोई समाधान दें जिसका उपयोग मैं बिना किसी उपयोगकर्ता इंटरैक्शन के एपीआई तक पहुंच सकता हूं।

अग्रिम में धन्यवाद।

{ 
    string token = GetOAuthTokenFromAAD(); 
    string requestURL = String.Format("{0}/{1}/{2}/{3}", 
        ConfigurationManager.AppSettings["ARMBillingServiceURL"], 
        "subscriptions", 
        ConfigurationManager.AppSettings["SubscriptionID"], 
        "providers/Microsoft.Commerce/RateCard?api-version=2015-06-01-preview&$filter=OfferDurableId eq 'MS-AZR-*****' and Currency eq 'INR' and Locale eq 'en-IN' and RegionInfo eq 'IN'"); 

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestURL); 

    request.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + token); 
    request.ContentType = "application/json"; 
    HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
    Console.WriteLine(String.Format("RateCard service response status: {0}", response.StatusDescription)); 
} 

public static string GetOAuthTokenFromAAD() 
{ 
     AuthenticationContext authenticationContext = new AuthenticationContext(string.Format("{0}/{1}",ConfigurationManager.AppSettings["ADALServiceURL"], ConfigurationManager.AppSettings["TenantDomain"])); 

     AuthenticationResult result = null; 
     ClientCredential uc = new ClientCredential(Client_Id, Secret_Key); 
     try 
     { 
      result = authenticationContext.AcquireToken("https://management.core.windows.net/", uc); 
     } 
     return result.AccessToken; 
} 

//App Config File 
<add key="ADALServiceURL" value="https://login.microsoftonline.com" /> 
<add key="ADALRedirectURL" value="http://*****-authentication.cloudapp.net" /> 
<add key="ARMBillingServiceURL" value="https://management.core.windows.net" /> 
<add key="TenantDomain" value="********.onmicrosoft.com" /> 
<add key="SubscriptionID" value="*******-****-****-****-********" /> 
<add key="ClientId" value="*******-****-****-****-********" /> 
+0

अपने कोड और भी आप Azure AD में बनाए गए आवेदन के लिए सेटिंग्स साझा करें। –

+0

पोस्ट में जोड़ा गया। – Tejas

+0

एक त्वरित प्रश्न - क्या आपने Azure AD में एप्लिकेशन को कॉन्फ़िगर करते समय सेवा प्रबंधन API संचालन निष्पादित करने की अनुमति दी है? –

उत्तर

1

अपडेट::

यहाँ मेरी कोड है मैं भी एक पुन: प्रयोज्य प्रमाणीकरण हेल्पर कक्षा लाइब्रेरी के रूप में इन तरीकों प्रदान की है। पासवर्ड दृष्टिकोण का उपयोग करने के लिए गैर-सहभागी आपको पहले नीचे पोस्ट की धारा का पालन करें "- PowerShell पासवर्ड से प्रमाणीकृत" की जरूरत है:: Authenticating a service principal with ARM

Azure Authentication - Authenticating any Azure API Request in your Application

विधि 1: आप इस लिंक पर एक ही प्राप्त कर सकते हैं

फिर टोकन लाने के लिए नीचे दिए गए कोड स्निपेट का उपयोग करें।

var authenticationContext = new AuthenticationContext(String.Format("{0}/{1}", 
                   ConfigurationManager.AppSettings["ADALServiceURL"], 
                   ConfigurationManager.AppSettings["TenantDomain"])); 
     var credential = new ClientCredential(clientId: "11a11111-11a1-111a-a111-1afeda2bca1a", clientSecret: "passwordhere"); 
     var result = authenticationContext.AcquireToken(resource: "https://management.core.windows.net/", clientCredential: credential); 

     if (result == null) 
     { 
      throw new InvalidOperationException("Failed to obtain the JWT token"); 
     } 

     string token = result.AccessToken; 

     return token; 

वैकल्पिक रूप से (विधि 2), आप भी प्रमाण पत्र विधि का उपयोग कर सकते हैं। उस स्थिति में उपरोक्त के समान लिंक का उपयोग करें, लेकिन उस लिंक से "प्रमाण पत्र के साथ प्रमाणीकरण - PowerShell" अनुभाग का पालन करें। तब टोकन गैर सहभागी लाने के लिए नीचे दिए गए कोड का उपयोग:

var subscriptionId = "1a11aa11-5c9b-4c94-b875-b7b55af5d316"; 
     string tenant = "1a11111a-5713-4b00-a1c3-88da50be3ace"; 
     string clientId = "aa11a111-1050-4892-a2d8-4747441be14d"; 

     var authContext = new AuthenticationContext(string.Format("https://login.windows.net/{0}", tenant)); 

     X509Certificate2 cert = null; 
     X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser); 
     string certName = "MyCert01"; 

     try 
     { 
      store.Open(OpenFlags.ReadOnly); 
      var certCollection = store.Certificates; 
      var certs = certCollection.Find(X509FindType.FindBySubjectName, certName, false); 
      //var certs = certCollection.Find(X509FindType.FindBySerialNumber, "E144928868B609D35F72", false); 
      if (certs == null || certs.Count <= 0) 
      { 
       throw new Exception("Certificate " + certName + " not found."); 
      } 
      cert = certs[0]; 
     } 
     finally 
     { 
      store.Close(); 
     } 

     var certCred = new ClientAssertionCertificate(clientId, cert); 
     var token = authContext.AcquireToken("https://management.core.windows.net/", certCred); 
     var creds = new TokenCloudCredentials(subscriptionId, token.AccessToken); 
     //var client = new ResourceManagementClient(creds); 
     return token.AccessToken; 
संबंधित मुद्दे