C# Generate token console for Intune datawarehouse

Chaumette Garcia 0 Reputation points
2024-03-07T16:46:48.8866667+00:00

Using .NET 4.8.1 and the code snippet below from Microsoft Learn (https://learn.microsoft.com/en-us/mem/intune/developer/data-warehouse-app-only-auth-). I am unable to get this module to work.

It seems the line with AuthenticationContext, ClientCredential and SecureClientSecret are outdated/depreciated. Example - error: ClientCredential' is obsolete: 'Use ConfidentialClientApplicationBuilder.WithCertificate or WithClientSecret instead. I cannot get this code snippet to work.

Can anyone help refine this?

Here's the complete class:


using System.Security;
using System.Configuration;
using Microsoft.Identity.Client;


class IntuneDataWarehouse
{
    public static void Main()
    {
        Console.WriteLine("Intune Datawarehouse Start");

        var applicationId = ConfigurationManager.AppSettings["appId"].ToString();
        SecureString applicationSecret = ConvertToSecureStr(ConfigurationManager.AppSettings["appKey"].ToString()); // Load as SecureString from configuration file or secret store (i.e. Azure KeyVault)
        var tenantDomain = ConfigurationManager.AppSettings["tenantDomain"].ToString();

        var msalContext = new AuthenticationContext($"https://login.windows.net/" + tenantDomain + "/oauth2/token");

         AuthenticationResult authResult = msalContext.AcquireTokenAsync(
         resource: "https://api.manage.microsoft.com/",
         clientCredential: new ClientCredential(
             applicationId,
             new SecureClientSecret(applicationSecret))).Result;

        Console.WriteLine("End of run");
    }

    private static SecureString ConvertToSecureStr(string appkey)
    {
        if (appkey == null)
            throw new ArgumentNullException("AppKey must not be null.");

        var secureAppKey = new SecureString();

        foreach (char c in appkey)
            secureAppKey.AppendChar(c);

        secureAppKey.MakeReadOnly();
        return secureAppKey;
    }
}
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,356 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,221 questions
Microsoft Intune Security
Microsoft Intune Security
Microsoft Intune: A Microsoft cloud-based management solution that offers mobile device management, mobile application management, and PC management capabilities.Security: The precautions taken to guard against crime, attack, sabotage, espionage, or another threat.
330 questions
0 comments No comments
{count} votes