我想仅使用REST Api与Azure进行通信,而不使用任何sdk组件 - 与新ARM通信 如何获取承载认证的承载令牌?我看到所有示例都使用SDK
答案 0 :(得分:0)
要获取授权令牌,您需要将Active Directory身份验证库安装到项目中。最简单的方法是使用NuGet package。
使用此代码获取令牌:
public static string GetAToken()
{
var authenticationContext = new AuthenticationContext("https://login.windows.net/{tenantId or tenant name}");
var credential = new ClientCredential(clientId: "{application 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;
}
Authenticating Azure Resource Manager requests有更多详细信息和代码示例。