2016-05-02 13 views
1

は、AzureのネットワークAPIを参照することによって点である:Azure for Network Rest APIでアクセストークンを取得する方法は?続い

次の情報は、すべてのタスクに共通である:2015年6月15日と

  1. 交換{APIバージョン}。
  2. {subscription-id}をURIのサブスクリプション識別子に置き換えます。
  3. {resource-group-name}をリソースグループに置き換えます。詳細については、リソースグループを使用したAzureリソースの管理を参照してください。
  4. Content-Typeヘッダーをapplication/jsonに設定します。
  5. Authorizationヘッダーを、Azure Active Directoryから取得するJSON Webトークンに設定します。

私は第5ポイントについて混乱しています。 Azure Active Directoryを使用してアクセストークンを取得する方法を教えてください。 RFCで定義されている

答えて

4

のAzure Active Directoryは、OAuth認証プロトコルに基づいて構築された6749 The OAuth 2.0 Authorization Framework

トークンを使用しての背後にある考え方は、あなたが中央機関に認証を行い、その後、必要とせず、別のシステムに与えられたアクセス権を持つことができるということです

そのシステムにあなたの資格情報を与えます。 Service to Service Calls Using Client Credentialsこの場合

taken from https://msdn.microsoft.com/en-gb/library/azure/dn645543.aspx

3で呼ばれていたサーバーが侵害された場合は、資格情報がまだ安全だろう、とトークンが期限切れになるまで、攻撃者が唯一のリソースへのアクセスを持っているでしょうから

。したがって、トークンは一般的に短命です。

次の内容

あなたがアクセストークンと利用を取ることができ、このことから、次の応答

{ 
"access_token":"eyJhbGciOiJSUzI1NiIsIng1dCI6IjdkRC1{shorted}", 
"token_type":"Bearer", 
"expires_in":"3599", 
"expires_on":"1388452167", 
"resource":"https://service.contoso.com/" 
} 

が生成されます

POST contoso.com/oauth2/token HTTP/1.1 
Host: login.microsoftonline.com 
Content-Type: application/x-www-form-urlencoded 

grant_type=client_credentials&client_id=625bc9f6-3bf6-4b6d-94ba-e97cf07a22de&client_secret=qkDwDJlDfig2IpeuUZYKH1Wb8q1V0ju6sILxQQqhJ+s=&resource=https%3A%2F%2Fservice.contoso.com%2F 

login.microsoftonline.comにPOSTリクエストをすることによってトークンを取得することができますそれはあなたのアプリケーションで。

これはAzureの認証フローであり、あらかじめ設定されたトークンを使用するだけでは変更できません。証明書を使用してもトークンを取得し、それを使用してリソースに対して権限を与えます。

+0

私はGoLangで働いています。AzureのREST APIをネットワーク管理に使用しています。私はまだアクセストークンを取得する方法を混同しています。私はすべての手順を完了し、AADを作成し、役割/許可を与えました。しかし、アクセストークンを取得する方法については固執しています。コードからアクセストークンを取得する方法はありますか? – Baber

+0

@Baber質問に対処するための回答を修正しました –

+0

ありがとうございます。それは本当に私をたくさん助けました。私はもう助けが必要な場合私はあなたをもう一度キャッチします:) – Baber

0

Azure ADサービスプリンシパルを作成してから、認証(JWT)トークンを取得する必要があります。以下のサンプルスクリプトは、PowerShellを使用してAzure ADサービスの原則を作成する方法を示しています。ウォークスルーの詳細については、https://azure.microsoft.com/en-us/documentation/articles/resource-group-authenticate-service-principal/#authenticate-service-principal-with-password—powershellのガイダンスを参照してください。 create a service principal via the Azure portalも可能です。

$pwd = “[your-service-principle-password]” 
$subscriptionId = “[your-azure-subscription-id]” 

Login-AzureRmAccount 
Select-AzureRmSubscription -SubscriptionId $subscriptionId 

$azureAdApplication = New-AzureRmADApplication ` 
         -DisplayName “ Demo Web name” ` 
         -HomePage “https://localhost/webdemo” ` 
         -IdentifierUris “https://localhost/webdemo” ` 
         -Password $pwd 

New-AzureRmADServicePrincipal -ApplicationId $azureAdApplication.ApplicationId 
New-AzureRmRoleAssignment -RoleDefinitionName Reader -ServicePrincipalName $azureAdApplication.ApplicationId 

$subscription = Get-AzureRmSubscription -SubscriptionId $subscriptionId 
$creds = Get-Credential -UserName $azureAdApplication.ApplicationId -Message “Please use your service principle credentials” 

Login-AzureRmAccount -Credential $creds -ServicePrincipal -TenantI $subscription.TenantId 

GoLangについて詳しくはありませんが、次の.NETコードを参照してアプリケーションのアクセストークンを取得することができます。

public static string GetAccessToken() 
{ 
    var authenticationContext = new AuthenticationContext("https://login.windows.net/{tenantId or tenant name}"); 
    var credential = new ClientCredential(clientId: "{client id}", clientSecret: "{application password}"); 
    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; 
} 
関連する問題