2

私は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 との接続を作成するには:

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;
}

を呼び出すとLoginWithPromptAsync、資格情報を尋ねるポップアップが表示されました。コードを実行するたびにこのポップアップを表示したくありません。Azure アプリを作成する以外に、このことを思いつく方法はありますか?

ApplicationIdTenantIdCertificateThumbprintおよびSubscriptionId(下) があります。これらのフィールドを使用して、プロンプトなしで Azure への認証を行うことはできますか?

資格証明の例

4

1 に答える 1

6

関数を使用して、UserTokenProvider.LoginSilentAsync(nativeClientAppClientid, domainName, userName, password)ポップアップなしで資格情報を取得できます。以下は私のテストコードです。WebAppの登録方法はドキュメントを参照してください。

    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. user1@contoso.org).</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;
    }

ここに画像の説明を入力

アップデート:

AD アプリを登録してアプリケーションにロールを割り当てる方法の詳細な手順については、ドキュメントを参照してください。その後tenantId, appId, secretKey、Azure ポータルから取得できます。次に、 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!
}

ここに画像の説明を入力

于 2016-12-22T06:03:59.193 に答える