Aja*_*jay 2 c# azure azure-management-api azure-api-apps
我在 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;
Run Code Online (Sandbox Code Playgroud)
创建与 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;
}
Run Code Online (Sandbox Code Playgroud)
打电话时LoginWithPromptAsync,我收到了弹出窗口,询问我的凭据。我不希望每次运行代码时都出现此弹出窗口。除了创建 Azure 应用程序之外,还有什么办法可以提出这个问题吗?
我有一个ApplicationId, TenantId, CertificateThumbprint, 和SubscriptionId(如下)。我可以使用这些字段在没有提示的情况下对 azure 进行身份验证吗?
我们可以使用该函数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;
}
Run Code Online (Sandbox Code Playgroud)
更新:
有关如何注册 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!
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5506 次 |
| 最近记录: |