2016-07-23 3 views
1

Я разрабатываю приложение, которое будет использовать API управления Azure, чтобы отображать информацию о виртуальных машинах, запуске, остановке виртуальной машины и т. Д.Ошибка аутентификации в API управления Azure

Я могу подтвердить подлинность пользователя, но когда я пытаюсь получить информацию о виртуальной машине это показывает,

пользователя не уполномочено выполнять Microsoft.Compute/virtualMachines/чтение

Но я админ на моей лазурной учетной записи, и у нее есть разрешение владельца + читателя. Я могу сделать то же самое, используя powershell, но не приложение.

я упомянул эту ссылку для развития:

https://azure.microsoft.com/en-in/documentation/articles/virtual-machines-windows-csharp-manage/

Мой пример кода ниже:

static void Main(string[] args) 
    { 
     var groupName = "XYZ"; 
     var vmName = "DC1"; 
     var location = "Southeast Asia"; 
     var subscriptionId = "My Subscription ID"; 

     var token = GetAccessTokenAsync(); 
     var credential = new TokenCredentials(token.Result.AccessToken); 

     GetVirtualMachineAsync( credential, groupName, vmName, subscriptionId); 
    } 

    private static async Task<AuthenticationResult> GetAccessTokenAsync() 
    { 
    var cc = new ClientCredential("{client-id}", "{client-secret}"); 
     var context = new AuthenticationContext("https://login.windows.net/{tenant-id}"); 
     var result = await context.AcquireTokenAsync("https://management.azure.com/", cc); 
     if (result == null) 
     { 
     throw new InvalidOperationException("Could not get the token"); 
     } 
     return result; 
    } 
    public static async void GetVirtualMachineAsync( TokenCredentials credential, string groupName, string vmName string subscriptionId) 
{ 
    Console.WriteLine("Getting information about the virtual machine..."); 

    var computeManagementClient = new ComputeManagementClient(credential) 
    { SubscriptionId = subscriptionId }; 
    var vmResult = await computeManagementClient.VirtualMachines.GetAsync(
     groupName, 
     vmName, 
     InstanceViewTypes.InstanceView); 

    Console.WriteLine("hardwareProfile"); 
    Console.WriteLine(" vmSize: " + vmResult.HardwareProfile.VmSize); 

    Console.WriteLine("\nstorageProfile"); 
    Console.WriteLine(" imageReference"); 
    Console.WriteLine(" publisher: " + vmResult.StorageProfile.ImageReference.Publisher); 
    Console.WriteLine(" offer: " + vmResult.StorageProfile.ImageReference.Offer); 
    Console.WriteLine(" sku: " + vmResult.StorageProfile.ImageReference.Sku); 
    Console.WriteLine(" version: " + vmResult.StorageProfile.ImageReference.Version); 
    Console.WriteLine(" osDisk"); 
    Console.WriteLine(" osType: " + vmResult.StorageProfile.OsDisk.OsType); 
    Console.WriteLine(" name: " + vmResult.StorageProfile.OsDisk.Name); 
    Console.WriteLine(" createOption: " + vmResult.StorageProfile.OsDisk.CreateOption); 
    Console.WriteLine(" uri: " + vmResult.StorageProfile.OsDisk.Vhd.Uri); 
    Console.WriteLine(" caching: " + vmResult.StorageProfile.OsDisk.Caching); 

    Console.WriteLine("\nosProfile"); 
    Console.WriteLine(" computerName: " + vmResult.OsProfile.ComputerName); 
    Console.WriteLine(" adminUsername: " + vmResult.OsProfile.AdminUsername); 
    Console.WriteLine(" provisionVMAgent: " + vmResult.OsProfile.WindowsConfiguration.ProvisionVMAgent.Value); 
    Console.WriteLine(" enableAutomaticUpdates: " + vmResult.OsProfile.WindowsConfiguration.EnableAutomaticUpdates.Value); 

    Console.WriteLine("\nnetworkProfile"); 
    foreach (NetworkInterfaceReference nic in vmResult.NetworkProfile.NetworkInterfaces) 
    { 
     Console.WriteLine(" networkInterface id: " + nic.Id); 
    } 

    Console.WriteLine("\nvmAgent"); 
    Console.WriteLine(" vmAgentVersion" + vmResult.InstanceView.VmAgent.VmAgentVersion); 
    Console.WriteLine(" statuses"); 
    foreach (InstanceViewStatus stat in vmResult.InstanceView.VmAgent.Statuses) 
    { 
     Console.WriteLine(" code: " + stat.Code); 
     Console.WriteLine(" level: " + stat.Level); 
     Console.WriteLine(" displayStatus: " + stat.DisplayStatus); 
     Console.WriteLine(" message: " + stat.Message); 
     Console.WriteLine(" time: " + stat.Time); 
    } 

    Console.WriteLine("\ndisks"); 
    foreach (DiskInstanceView idisk in vmResult.InstanceView.Disks) 
    { 
     Console.WriteLine(" name: " + idisk.Name); 
     Console.WriteLine(" statuses"); 
     foreach (InstanceViewStatus istat in idisk.Statuses) 
     { 
      Console.WriteLine(" code: " + istat.Code); 
      Console.WriteLine(" level: " + istat.Level); 
      Console.WriteLine(" displayStatus: " + istat.DisplayStatus); 
      Console.WriteLine(" time: " + istat.Time); 
     } 
    } 

    Console.WriteLine("\nVM general status"); 
    Console.WriteLine(" provisioningStatus: " + vmResult.ProvisioningState); 
    Console.WriteLine(" id: " + vmResult.Id); 
    Console.WriteLine(" name: " + vmResult.Name); 
    Console.WriteLine(" type: " + vmResult.Type); 
    Console.WriteLine(" location: " + vmResult.Location); 
    Console.WriteLine("\nVM instance status"); 
    foreach (InstanceViewStatus istat in vmResult.InstanceView.Statuses) 
    { 
     Console.WriteLine("\n code: " + istat.Code); 
     Console.WriteLine(" level: " + istat.Level); 
     Console.WriteLine(" displayStatus: " + istat.DisplayStatus); 
    } 

} 

Спасибо.

ответ

0

Я сам решил эту проблему. Мне не хватало, чтобы предоставить соответствующие права на приложение, созданное в активном каталоге, используя лазурный портал. В моем случае я дал владельцу доступ.

Смежные вопросы