【问题标题】:Azure API AuthenticationAzure API 身份验证
【发布时间】:2016-12-21 10:22:00
【问题描述】:

我在 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 应用之外,还有什么办法可以想出这个东西吗?

我有一个ApplicationIdTenantIdCertificateThumbprintSubscriptionId(如下)。我可以使用这些字段在没有提示的情况下对 Azure 进行身份验证吗?

【问题讨论】:

标签: c# azure azure-api-apps azure-management-api


【解决方案1】:

我们可以使用函数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. 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 App和为应用分配角色的详细步骤,请参考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!
}

【讨论】:

  • 我正在使用 Azure API 进行 ADLS 文件访问和一些文件操作,并希望与 U-SQL 项目集成。在这种情况下,“域名”是什么?其次。我不想在 Azure 门户上注册应用程序。
  • Azure 还允许使用自签名管理证书或 Azure AD 来获取身份验证。有关 Windows Azure 管理证书的更多信息,请参阅document。域名:By default, a basic domain name at .onmicrosoft.com is included with your directory. Later, you can add a domain name that your organization already uses, such as contoso.com.
  • 我有应用程序 ID、TenantId 和其他东西。这会帮助我在没有提示的情况下验证到 azure。我已经编辑了我的问题并附上了一张图片..你能帮我解决这个问题吗..谢谢...
  • 感谢您的努力...我将您的代码与 AcquireTokenAsync 函数一起使用。由于该方法是异步的,因此控制不会到达下一行“var accessToken = tokenResponse.AccessToken;”或不产生任何身份验证错误......
  • 嗨 Ajay,var tokenResponse = context.AcquireTokenAsync("https://management.azure.com/", clientCredential).Result 应该让我们得到 tokenResponse。
猜你喜欢
  • 1970-01-01
  • 2020-10-08
  • 2017-11-12
  • 2016-08-28
  • 1970-01-01
  • 2015-02-02
  • 2018-07-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多