如何使用 .Net SDK 从恢复服务保管库生成安全 PIN?

How to generate Security PIN from Recovery Services vault using .Net SDK?

目前我正在尝试使用以下代码但得到 System.NullReferenceException: 'Object reference not set to an instance of an object.'.

using Microsoft.Azure.Management.RecoveryServices.Backup;
public static ISecurityPINsOperations SecurityPINs { get; }
public static class GenerateSecurityPIN
    {
        private static readonly ISecurityPINsOperations Operations1;
        var pin = await SecurityPINs.GetAsync("rsv-itops-azure-support", "rg-azure-itops-rnd-dev");
    }

引用以下链接: https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.management.recoveryservices.backup.securitypinsoperationsextensions?view=azure-dotnet

您应该使用以下命令安装 Microsoft.Azure.Management.RecoveryServices.Backup -Version 4.1.5-preview

Install-Package Microsoft.Azure.Management.RecoveryServices.Backup -Version 4.1.5-preview

然后你的示例代码,应该是这样的,你需要先创建RecoveryServicesBackupClient

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.Management.RecoveryServices.Backup;
using Microsoft.Azure.Management.ResourceManager.Fluent;

namespace ConsoleApp2
{
    static class Program
    {
        static void Main(string[] args)
        {
            var subscriptionId = "";
            var vaultname = "";
            var resourcegroup = "";
            var credentials = SdkContext.AzureCredentialsFactory.FromFile(@"Auth file path");
            var client = new RecoveryServicesBackupClient(credentials) { SubscriptionId = subscriptionId };
            var pin = client.SecurityPINs.GetAsync(vaultname,resourcegroup).Result.SecurityPIN;
        }
    }
}

如果你不想升级包,你也可以use restapi to get PIN

相关post: