2016-12-21 19 views
0

は、私は自分の資格情報を尋ねるポップアップを得、これを通じAzureのAPI認証

private static ServiceClientCredentials AuthenticateAzure(string domainName, string nativeClientAppCLIENTID) 
    { 
     // User login via interactive popup 
     SynchronizationContext.SetSynchronizationContext(new SynchronizationContext()); 
     // Use the client ID of an existing AAD "Native Client" application. 
     var activeDirectoryClientSettings = ActiveDirectoryClientSettings.UsePromptOnly(nativeClientAppCLIENTID, new Uri("urn:ietf:wg:oauth:2.0:oob")); 
     return UserTokenProvider.LoginWithPromptAsync(domainName, activeDirectoryClientSettings).Result; 
    } 

をC#コードでAzureのAPIを使用して、図書館

using Microsoft.Rest; using Microsoft.Rest.Azure.Authentication; 

using Microsoft.Azure.Management.DataLake.Store; 

using Microsoft.Azure.Management.DataLake.StoreUploader; 

using Microsoft.Azure.Management.DataLake.Analytics; 

using Microsoft.Azure.Management.DataLake.Analytics.Models; 

using Microsoft.WindowsAzure.Storage.Blob; 

アズールとの接続を作成するには、以下の使用しています。私は毎回このポップアップが表示されないようにします。 Azureアプリケーションを作成する以外にこの方法を考え出す方法はありますか?

======================================

私は、アプリケーションのIDを持っています、TenantIdと他のもの。これはプロンプトなしで紺碧への認証に役立ちますか?

enter image description here

+0

このhttp://blog.davidebbo.com/2015/12/calling-arm-usingを行うにはServicePrinicpalを作成する方法のように、ここでの命令があります-plain-rest.html –

答えて

3

我々は、ポップアップせずに私たちの資格情報を取得する機能UserTokenProvider.LoginSilentAsync(nativeClientAppClientid, domainName, userName, password)を使用することができます。それは私のためにうまくいく、私のテストコードは次のとおりです。レジストリWebAppについては、documentを参照してください。

static void Main(string[] args) 
    { 
     var certificate = AuthenticateAzure("your domain name", "Ad App client ID", "username", "password"); 
    } 

    /// <summary> 
    /// Log in to azure active directory in non-interactive mode using organizational 
    // id credentials and the default token cache. Default service settings (authority, 
    // audience) for logging in to azure resource manager are used. 
    /// </summary> 
    /// <param name="domainName"> The active directory domain or tenant id to authenticate with</param> 
    /// <param name="nativeClientAppClientid"> The active directory client id for this application </param> 
    /// <param name="userName"> The organizational account user name, given in the form of a user principal name (e.g. [email protected]).</param> 
    /// <param name="password"> The organizational account password.</param> 
    /// <returns>A ServiceClientCredentials object that can be used to authenticate http requests using the given credentials.</returns> 
    private static ServiceClientCredentials AuthenticateAzure(string domainName, string nativeClientAppClientid,string userName,string password) 
    { 
     return UserTokenProvider.LoginSilentAsync(nativeClientAppClientid, domainName, userName, password).Result; 
    } 

enter image description here

更新:ADアプリケーションをレジストリやアプリケーションに役割を割り当てる方法について

その他詳細手順、documentを参照してください。 その後AzureポータルからtenantId, appId, secretKeyを取得できます。次に、Microsoft.IdentityModel.Clients.ActiveDirectory SDKを使用して、api認証用のトークンを取得できます。

デモコード:

var subscriptionId = "Your subscrption"; 
var appId = "Registried Azure Application Id"; 
var secretKey = "Secret Key"; 
var tenantId = "tenant Id"; 
var context = new AuthenticationContext("https://login.windows.net/" + tenantId); 
ClientCredential clientCredential = new ClientCredential(appId, secretKey); 
var tokenResponse = context.AcquireTokenAsync("https://management.azure.com/", clientCredential).Result; 
var accessToken = tokenResponse.AccessToken; 
using (var client = new HttpClient()) 
{ 
    client.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessToken); 
    client.BaseAddress = new Uri("https://management.azure.com/"); 
    // Now we can party with our HttpClient! 
} 

enter image description here

+0

私はAzure APIをADLSファイルアクセスやファイル操作に使用しており、U-SQLプロジェクトと統合したいと考えています。その場合、「ドメイン名」は何ですか?第二に。私はAzureポータルにアプリケーションを登録したくありません。 – Ajay

+0

Azureでは、自己署名管理証明書またはAzure ADを使用して認証を取得することもできます。 Windows Azure管理証明書の詳細については、[ドキュメント](https://www.simple-talk.com/cloud/security-and-compliance/windows-azure-management-certificates/)を参照してください。ドメイン名: 'デフォルトでは、.onmicrosoft.comの基本ドメイン名がディレクトリに含まれています。その後、contoso.comなど組織がすでに使用しているドメイン名を追加することができます。 ' –

+0

私はアプリケーションID、TenantIdなどを持っています。プロンプトを表示せずに紺碧まで認証するのに役立ちますか?私は自分の質問を編集し、1つのイメージを添付しました。この行で私を助けてくれますかありがとう... – Ajay

関連する問題