2016-06-02 10 views
2

私は、認証の認可を伴うAzure管理APIの認可に問題があります。 Microsoft.Azure.Management.Sqlを使用すると、エラーが発生します。「AuthenticationFailedInvalidHeader:認証に失敗しました。「Authorization」ヘッダーが無効な形式で提供されています。しかし、Microsoft.WindowsAzure.Management.Sqlをほぼ同じコードで使用すると、すべて正常に動作しますが、これはこのライブラリの古いバージョンです。私は弾性プールをサポートしていないような古い外観のため、私は新しいバージョンが必要です。Azure Management API X509Certificate2を使用した認証の問題

using System; 
using System.Collections.Generic; 
using System.Security.Cryptography.X509Certificates; 
using Microsoft.WindowsAzure; 
using Microsoft.WindowsAzure.Management.Sql; 
using Microsoft.WindowsAzure.Management.Sql.Models; 

namespace Test2 
{ 
    class Program 
    { 
     private static ServerListResponse servers; 
     private static string _resourceGroupName = "xxx"; 
     private static string subscriptionId = "xxx"; 
     private static string certThumbprint = "xxx"; 

     static void Main(string[] args) 
     { 
      X509Certificate2 cert = GetCertificate(certThumbprint); 

      SubscriptionCloudCredentials credentials = new CertificateCloudCredentials(subscriptionId, cert); 
      SqlManagementClient client = new SqlManagementClient(credentials); 

      servers = client.Servers.List(); 

      Console.ReadKey(); 
     }    
    } 
} 

うまくこの作品は、この新しいAPIは、典型的なシナリオは、クライアントID /秘密またはユーザー名/パスワードを使用することであるリソースマネージャを使用していますエラー

using System; 
using System.Collections.Generic; 
using System.Security.Cryptography.X509Certificates; 
using System.Threading.Tasks; 
using Microsoft.Azure; 
using Microsoft.Azure.Management.Sql; 
using Microsoft.Azure.Management.Sql.Models; 

namespace Test2 
{ 
    class Program 
    { 
     private static ServerListResponse servers; 
     private static string _resourceGroupName = "xxx"; 
     private static string subscriptionId = "xxx"; 
     private static string certThumbprint = "xxx"; 

     static void Main(string[] args) 
     { 
      X509Certificate2 cert = GetCertificate(certThumbprint); 

      SubscriptionCloudCredentials credentials = new CertificateCloudCredentials(subscriptionId, cert); 
      SqlManagementClient client = new SqlManagementClient(credentials); 

      Task.Run(async() => 
      { 
       servers = await client.Servers.ListAsync(_resourceGroupName); 
      }).Wait(); 

      Console.ReadKey(); 
     }    
    } 
} 
+0

これを参照してください。http://stackoverflow.com/questions/37570328/having-problems-with-azure-virtual-machines-and-rest-api 。基本的にAzure Resource Manager APIとAzure Service Management APIを混在させています。 –

答えて

0

を生成認証。

確かに可能な証明書に固執したいが、設定するには少しの作業が必要だと言われている。 ARMは基本的にアプリケーションを「スルー」して認証し、そのアプリケーションでpkiを構成することができます。私は、コンフィギュレーション(PowerShellを使用してますが伝達されなければならない)しばらく前に書いた:Microsoft.Azureについては

http://hindenes.com/trondsworking/2015/07/19/certificate-based-authentication-to-azure-resource-manager-using-powershell/

1

、彼らは承認戦略を変更します。 X509Certificatesはそのようにサポートされなくなりました。ただし、ADALは対話型ログインまたはサービスプリンシパルで使用できます。次のようにして上記のプログラムでGetTokenCloudCredentials方法を交換

インタラクティブログイン:

using System; 
using System.Security; 
using Microsoft.Azure.Management.Sql; 
using Microsoft.IdentityModel.Clients.ActiveDirectory; 
using Microsoft.Azure; 

namespace GetSqlARM 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var token = GetTokenCloudCredentials(); 
      SqlManagementClient client = new SqlManagementClient(token); 

      var server = client.Servers.Get("<Your Resource Group>", "<Your Sql Server>"); 

      System.Console.WriteLine(server.ToString()); 
      System.Console.WriteLine("Press ENTER to continue"); 
      System.Console.ReadLine(); 
     } 

     public static TokenCloudCredentials GetTokenCloudCredentials() 
     { 
      String tenantID = "<Your Tenant ID>"; 
      String loginEndpoint = "https://login.windows.net/"; 
      Uri redirectURI = new Uri("urn:ietf:wg:oauth:2.0:oob"); 
      String clientID = "1950a258-227b-4e31-a9cf-717495945fc2"; 
      String subscriptionID = "<Your Subscription ID>"; 
      String resource = "https://management.core.windows.net/"; 
      String authString = loginEndpoint + tenantID; 

      AuthenticationContext authenticationContext = new AuthenticationContext(authString, false); 

      var promptBehaviour = PromptBehavior.Auto; 

      var userIdentifierType = UserIdentifierType.RequiredDisplayableId; 

      var userIdentifier = new UserIdentifier("<Your Azure Account>", userIdentifierType); 

      var authenticationResult = authenticationContext.AcquireToken(resource, clientID, redirectURI, promptBehaviour, userIdentifier); 

      return new TokenCloudCredentials(subscriptionID, authenticationResult.AccessToken); 
     } 
    } 
} 

サービスプリンシパルここ

は、サンプルコードです。サービスプリンシパルコードを使用するためには

public static TokenCloudCredentials GetTokenCloudCredentials() 
    { 
     String tenantID = "<Your Tenant ID>"; 
     String loginEndpoint = "https://login.windows.net/"; 
     String subscriptionID = "<Your Subscription ID>"; 
     String authString = loginEndpoint + tenantID; 
     String clientID = "<Your Client ID>"; 
     String key = "<Your Client Key>"; 
     var clientCred = new ClientCredential(clientID, key); 
     String resource = "https://management.core.windows.net/"; 

     AuthenticationContext authenticationContext = new AuthenticationContext(authString, false); 

     var authenticationResult = authenticationContext.AcquireToken(resource, clientCred); 

     return new TokenCloudCredentials(subscriptionID, authenticationResult.AccessToken); 
    } 

、あなたはサービスプリンシパルを作成するためにthis articleに従う必要があります。

  • Microsoft.Azure.Management.Sql、v0.46.0-プレリリース
  • Microsoft.IdentityModel.Clients.ActiveDirectory、v2.26.305102204
  • :これらのパッケージの場合

    、私は次のバージョンを使用しています

  • Microsoft.Rest.ClientRuntime.Azure、V3.2.0
関連する問題