我正尝试使用C#以编程方式从Microsoft.Azure.Management.Compute
检索hostedservices。这需要ServiceClientCredential
,我不知道如何得到它。谁能帮帮我吗?我能够使用Microsoft.WindowsAzure.Management.Compute
来获取它们,但是它仅返回ResourceManager下的实例,而不是经典实例。如何创建serviceclientcredential以便与Microsoft.azure.Management.compute一起使用
3
A
回答
6
1您需要创建Active Directory应用程序。使用此链接创建AD应用https://azure.microsoft.com/en-us/documentation/articles/resource-group-create-service-principal-portal/
示例代码使用Microsoft.Azure.Management.Compute 13.0.1 - 抢鲜SDK 网址:https://www.nuget.org/packages/Microsoft.Azure.Management.Compute/13.0.1-prerelease
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);
}
}
,这里是如何初始化客户端
netClient = new Microsoft.Azure.Management.Compute.ComputeManagementClient(new CustomLoginCredentials());
netClient.SubscriptionId = _subscriptionId;
+0
为什么我们需要在InitializeServiceClient中做这么多事情?假设我们不使用客户端来执行此操作,似乎最好创建一个可以获取必要资源并生成令牌提供程序的构造函数。 'ProcessHttpRequestAsync'然后可以从auth上下文获取新的令牌(以确保它是最新的当前令牌) – ElFik
相关问题
- 1. 创建一个C++静态库以便在Android上与Java一起使用
- 2. 如何下载AWS证书以便与NodeJS一起使用
- 3. 如何在Windows上安装curl以便与C一起使用
- 4. 如何连接UIPageControl,以便与其他控件一起使用?
- 5. 我应该如何构建我的数据以便与Firebase一起使用?
- 6. 使用nodejs创建REST API以与contentBlocks一起使用(createjs.org)
- 7. 如何创建水平或垂直支柱和胶水以便与Scala BoxPanel一起使用?
- 8. 如何模拟Spring SecurityContext,以便我可以将它与TestNG一起使用?
- 9. 如何使用插入创建表以便列可以为空?
- 10. 数据流 - 如何创建一个PCollectionView以与DoFnTester一起使用?
- 11. 如何使Zxing.Net的便携库与mvvmcross一起使用?
- 12. 构建PyQt以与Qt5一起使用
- 13. 创建SQLCommand以便在SQLDataAdapter中使用
- 14. 如何获取Office 365用户的ActorIds以便与GQL一起使用?
- 15. 如何创建与Highcharts一起使用的特定Json输出?
- 16. 如何创建可与TransactionScope一起使用的类?
- 17. 如何创建离线数据库与C++一起使用
- 18. 如何创建与MultiPeerConnectivity一起使用的SecIdentityRef?
- 19. 从Flask视图创建JSON文件以与D3一起使用
- 20. 创建Resources.Load变量以与多个按钮一起使用
- 21. 创建解析字典以与sys.argv一起使用
- 22. 如何使用软以便与JOIN
- 23. 如何创建Firebase的服务帐户以与Firebase函数一起使用
- 24. 将编码日期设为ulong以便与AmiBroker一起使用
- 25. 在HTML中嵌入csv以便与D3.js一起使用
- 26. 扩展MapField以便与自定义地图一起使用webservice
- 27. 将CURL命令转换为PHP以便与Laravel一起使用
- 28. Webpack:生成index.html以便与Django一起使用
- 29. 在Windows上编译V8以便与g ++一起使用
- 30. 在MacOS Sierra上安装opencv-3.1.0以便与python一起使用
事实上,由于某些原因,微软决定只提供有关这些库的最小文档。这是非常混乱的,尤其是经典和资源管理器显然(?)在不同的库中处理,混淆了像Microsoft.Azure.Management.Compute(做一件事)与Microsoft.WindowsAzure.Management.Compute(做了不同的事情) – Nik
我最近几天一直在试图弄清楚这一点,而且我越读它们的文档,越是感到困惑! – k2ibegin