Calling Azure REST API from .Net Core native application by displaying the Azure Login Popup from C#

David Downing 701 Reputation points
2020-02-17T04:36:23.217+00:00

Is it possible to initiate the Azure Login Popup from a native windows application?

Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,380 questions
{count} votes

Accepted answer
  1. David Downing 701 Reputation points
    2020-02-20T17:32:43.003+00:00

    I was able to get the Azure REST APIs working using the following code from the Fluent NuGet Packages:

        private static string TestAuth()  
        {  
            // AzureCredentials credentials = new AzureCredentials(spLogin, TenantID, AzureEnvironment.AzureGlobalCloud);  
            var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(  
                ClientID,  
                ClientSecret,  
                TenantID,  
                AzureEnvironment.AzureGlobalCloud);  
    
            var client = RestClient  
                .Configure()  
                .WithEnvironment(AzureEnvironment.AzureGlobalCloud)  
                .WithCredentials(credentials)  
                .Build();  
    
            CancellationToken cancellationToken = new CancellationToken();  
    
            var request = new HttpRequestMessage(HttpMethod.Get, $"https://management.azure.com/subscriptions/{SubscriptionID}/resourcegroups/{ResourceGroupName}?api-version=2019-10-01");  
            client.Credentials.ProcessHttpRequestAsync(request, cancellationToken).GetAwaiter().GetResult();  
            var httpClient = new HttpClient();  
            var response = httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, cancellationToken).GetAwaiter().GetResult();  
    
            return response.Content.ReadAsStringAsync().GetAwaiter().GetResult();  
        }  
    
    3 people found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Mehmet Seckin 6 Reputation points
    2020-02-17T10:33:19.757+00:00

    If I'm understanding this correctly, you want to make calls to the Azure Resource Management APIs from a native application. It is certainly possible to authenticate against Azure from a native windows application through Azure Active Directory.

    You can create an Azure AD application with user_impersonation permissions, and use that application to authenticate against your tenant using MSAL.NET libraries.

    There is a quickstart application that uses a similar approach to access Microsoft Graph APIs.

    Hope this helps.

    1 person found this answer helpful.