Search code examples
azure-sdk-.netazure-management-api

How do you authenticate using Azure Management Fluent API


How do we authenticate using service principal with Azure Managment Fluent API. I get the following error. Am I missing something?

Microsoft.IdentityModel.Clients.ActiveDirectory.AdalServiceException: 'AADSTS70002: Error validating credentials. AADSTS50012: Invalid client secret is provided.

        var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(ClientId, ClientSecret, AzureTenantId, AzureEnvironment.AzureGlobalCloud);

        var azure = Microsoft.Azure.Management.Fluent.Azure
            .Configure()
            .Authenticate(credentials)
            .WithSubscription(subscriptionid);

        var windowsVM = azure.VirtualMachines.Define("myWindowsVM")
            .WithRegion(Region.USWest)
            .WithNewResourceGroup(rgName)
            .WithNewPrimaryNetwork("10.0.0.0/28")
            .WithPrimaryPrivateIPAddressDynamic()
            .WithNewPrimaryPublicIPAddress("mywindowsvmdns")
            .WithPopularWindowsImage(KnownWindowsVirtualMachineImage.WindowsServer2012R2Datacenter)
            .WithAdminUsername("tirekicker")
            .WithAdminPassword(password)
            .WithSize(VirtualMachineSizeTypes.StandardD3V2)
            .Create();

Solution

  • AADSTS50012: Invalid client secret is provided

    According to the exception information, it seems that you supplied the invalid client secret.

    You could get the clientId, clientSecret, tenantId from the Azure Active Directory from Azure portal.

    More detail you could refer to Get application ID and authentication key and Get tenant ID.

    You also could get more information about how to Create an Azure Active Directory application from mentioned link.

    Note: You also need to Assign create application to role