我正在尝试使用C#在Azure上获取我的VMS列表。我已经找到了这个掘金包Microsoft.Azure.Management.Compute
,但是我无法创建它的客户端。我尝试过的:
public class CustomLoginCredentials : ServiceClientCredentials
{
private string AuthenticationToken { get; set; }
public override void InitializeServiceClient<T>(ServiceClient<T> client)
{
var authenticationContext =
new AuthenticationContext("https://login.windows.net/{tenantID}");
var credential = new ClientCredential(clientId: "xxxxx-xxxx-xx-xxxx-xxx", clientSecret: "{clientSecret}");
var result = authenticationContext.AcquireToken(resource: "https://management.core.windows.net/",
clientCredential: credential);
if (result == null)
{
throw new InvalidOperationException("Failed to obtain the JWT token");
}
AuthenticationToken = result.AccessToken;
}
public override async Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
if (AuthenticationToken == null)
{
throw new InvalidOperationException("Token Provider Cannot Be Null");
}
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", AuthenticationToken);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
//request.Version = new Version(apiVersion);
await base.ProcessHttpRequestAsync(request, cancellationToken);
}
}
但是我在此类上遇到编译错误,指出找不到AuthenticationContext
名称空间。我找不到有关如何在文档上创建此简单客户端的信息。
答案 0 :(得分:0)
您是否正在设置服务主体(SP)?可以在以下位置找到我们使用SP进行SDK身份验证的文档:https://docs.microsoft.com/en-us/dotnet/azure/dotnet-sdk-azure-authenticate?view=azure-dotnet#mgmt-auth
您需要将以下nuget包添加到您的项目中。
Install-Package Microsoft.Azure.Management.Fluent
Install-Package Microsoft.Azure.Management.ResourceManager.Fluent
答案 1 :(得分:0)
这是您需要的nuget软件包:https://www.nuget.org/packages/Microsoft.IdentityModel.Clients.ActiveDirectory/
Compute
nuget对于特定于VM的东西是必需的。要进行身份验证,您需要我发布的身份。