我使用 .NET 框架 4.7.2 创建了一个控制台应用程序,以使用这些 nuget 包连接到 Azure 资源管理器 API:
Microsoft.Azure.Management.ResourceManager.Fluent:v1.18.0 | 下载链接
Microsoft.Azure.Management.Fluent:v1.18.0 | 下载链接
这是代码:
namespace AzResourceManager
{
class Program
{
static void Main(string[] args)
{
var clientId = "********-****-****-****-************";
var subscriptionId = "********-****-****-****-************";
var tenantId = "********-****-****-****-************";
var cert = GetCertificate("********************************");
var creds = new AzureCredentialsFactory().FromServicePrincipal(clientId, cert, tenantId, AzureEnvironment.AzureGlobalCloud);
var azure = Azure.Authenticate(creds).WithSubscription(subscriptionId);
foreach (var rGroup in azure.ResourceGroups.List())
{
Console.WriteLine(rGroup.Name);
}
}
private static X509Certificate2 GetCertificate(string thumbPrint)
{
var certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
certStore.Open(OpenFlags.ReadOnly);
try
{
var certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint, thumbPrint, false);
if (certCollection.Count <= 0)
throw new InvalidOperationException("Unable to load certificate from store");
return certCollection[0];
}
finally
{
certStore.Close();
}
}
}
}
当执行到达这一行时
foreach (var rGroup in azure.ResourceGroups.List())
应用程序抛出一个System.NullReferenceException: 'Object reference not set to an instance of an object.'
我在 Azure Active Directory 和笔记本电脑中注册的应用程序中安装了证书。我尝试更新一些软件包,但结果是一样的。我唯一无法更新到最新版本的软件包是Microsoft.IdentityModel.Clients.ActiveDirectory(最新版本:v4.4.1),我只能将其更新到版本 v3.19.8
12345678_0001
喵喔喔
相关分类