AzureAD / microsoft-authentication-library-for-dotnet

Microsoft Authentication Library (MSAL) for .NET
https://aka.ms/msal-net
MIT License
1.4k stars 341 forks source link

[Bug] System.PlatformNotSupportedException when using AcquireTokenOnBehalfOf in Blazor Standalone WebAssembly App project #4940

Open LALITAMITTAL18 opened 1 month ago

LALITAMITTAL18 commented 1 month ago

Library version used

4.64.1

.NET version

Net 8.0

Scenario

PublicClient - desktop app

Is this a new or an existing app?

None

Issue description and reproduction steps

we are creating a office add in using Blazor Standalone WebAssembly App . I get the office token and trying to exchange it using AcquireTokenOnBehalfOf but I get System.PlatformNotSupportedException when using AcquireTokenInteractive

Relevant code snippets

Get the office token =

export function getOfficeAuthTokenAsync() {
    return new Promise((resolve, reject) => {
        try {
            Office.onReady(function () {               
                const authContext = Office.context.auth;

                authContext.getAccessTokenAsync(function (result) {

                    if (result.status === Office.AsyncResultStatus.Succeeded) {

                        const token = result.value;
                        resolve(token);
                    } else {

                        reject(result.error);
                    }
                });
            });     

        } catch (e) {
            console.log("e: " + e);
            reject(e);
        }
    });

Exchanging it -

IConfidentialClientApplication app =  ConfidentialClientApplicationBuilder.Create("client id")
                                                   .WithClientSecret("client sercret")
                                                   .WithAuthority($"https://login.microsoftonline.com/{tenantId}")
                                                   .Build();

    var scopeDefault = new string[] {"https://graph.microsoft.com/.default"};
    var tokenResponse = await app.AcquireTokenOnBehalfOf(scopeDefault, assert).ExecuteAsync();
    return tokenResponse.AccessToken;
}

Expected behavior

I should get a token

Identity provider

Microsoft Entra ID (Work and School accounts and Personal Microsoft accounts)

Regression

No response

Solution and workarounds

No response

bgavrilMS commented 1 month ago

Hi @LALITAMITTAL18 - the blazor webassembly version uses MSAL Javascript, not MSAL .NET. Please log an issue to https://github.com/AzureAD/microsoft-authentication-library-for-js

LALITAMITTAL18 commented 1 month ago

No I am using msal .net library. I have created a service and injected in program.cs. And calling the service in a razor component. It’s all .net code. I am just getting office add in token in js and for that I am using jsinterop and passing to my services for exchange. This is the smaple from MS on which my code is based on - https://github.com/OfficeDev/Office-Add-in-samples/tree/main/Samples/blazor-add-in/word-blazor-add-in/word-blazor-add-in

LALITAMITTAL18 commented 1 month ago

@bgavrilMS could you please open the ticket again. This is not closed. or let me know if I have to create a new ticket.

bgavrilMS commented 1 month ago

@LALITAMITTAL18 - I know you're dealing with C# code only, but Blazor is just wrapping JS code behind a C# interface.

Where exactly do you call AcquireTokenInteractive?

LALITAMITTAL18 commented 1 month ago

I call AcquireTokenOnBehalfOf in a class AuthenticationManager.cs and that class I inject in a razor component and use it. This whole thing work in asp.net core app with razor pages using same jsinterop. I created a teams app ang get teams token using jsinterop from js file to cs file and provide it to my authentication manager for AcquireTokenOnBehalfOf method.

bgavrilMS commented 1 month ago

What is the full stack trace? MSAL .NET doesn't throw PlatformNotSupportedException afaik,