How to get authorization code dynamically depending on tenant ID using blazer Webassembly

Jacob Duncan 5 Reputation points
2023-06-06T19:49:03.98+00:00

I am looking to dynamically create a Microsoft graph client at runtime where the tenant ID is changed dynamically every time it is created. I need to get a authorization code that is dependent on the tenant ID at runtime but I can not seem to make it work. I have tried both the Microsoft.Identity.Client; library and the Azure.Identity library but kept getting the error below. Does anyone know how I could achieve this or get around this error?

var authCodeCredential = new ClientSecretCredential (

    tenantId, clientId, clientSecret);




var graphClient = new GraphServiceClient(authCodeCredential, scopes);

Screenshot 2023-06-06 104244

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
10,587 questions
Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,387 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,254 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,244 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,466 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 55,686 Reputation points
    2023-06-06T22:33:40.3033333+00:00

    it does not seem partially secure to use client secret with a web assembly. I'd probably supply a webapi to do the work.

    You are correct, the HttpClient defined for blazor WASM is a subset of the actually HttpClient, as it uses js interop to create a tcp connection (WASM runs in a sandbox with no network, dom or file support). You could use the javascript graph api sdk and call with js interop, or using HttpClient call the Graph REST api directly rather than the sdk. It better documented anyway.

    https://learn.microsoft.com/en-us/graph/api/overview?view=graph-rest-1.0

    to get token with the REST api see:

    https://learn.microsoft.com/en-us/graph/auth-v2-service?tabs=http


  2. Jacob Duncan 5 Reputation points
    2023-06-08T13:25:31.78+00:00

    For anyone looking to achieve the same thing it is not directly possible to use this route as of 2023-06-08, at least that's what 4 days of debugging told me. I managed to achieve the desired result using Azure Functions as it can run server side code and make the calls on its end and requires 0 maintaining from us which was the goal. Appreciate the response though Bruce!

    0 comments No comments