Skip to content

Error retrieving the current token in a Blazor application after successful authentication in Azure B2C with Msal #39311

Closed
@vsfeedback

Description

@vsfeedback

This issue has been moved from a ticket on Developer Community.


I have created a Blazor application and I use Msal to do authentication following the examples from Azure B2C.
With my B2C configuration in the server, I can successfully login, I get the User, the claims and I can call the api.
I would need to have access to the token during development to be able to use that token to call my api services directly using swagger. So during Debugging I wanted to print the token on screen to copy and paste.

However, when I try to get the token in the Client after successful login, I get an Exception:
An exception occurred executing JS interop: The JSON value could not be converted to System.DateTimeOffset. Path: $.token.expires

This is a very confusing exception and doesn't seem to explain the type of error that is occurring. It seems that is not doing any validation during the parsing or pre-parsing. So not sure why this is happening.

More details:

I initialise with this:

builder. Services.AddMsalAuthentication(options =>
            {
                builder. Configuration.Bind("AzureAdB2C", options. ProviderOptions.Authentication);

//options. ProviderOptions.DefaultAccessTokenScopes.Add("02200220-20202-2020-2020-202020200202002"); // I tried to put the application Id a per documentation and it doesn't work

options. ProviderOptions.DefaultAccessTokenScopes.Add("openid");
                options. ProviderOptions.DefaultAccessTokenScopes.Add("offline_access");

// request scope to access the API
                options. ProviderOptions.AdditionalScopesToConsent.Add("https://myb2c.onmicrosoft.com/whateverApp/MyAPI");

options. ProviderOptions.LoginMode = "redirect";
            });

I also use a custom AuthorizationMessageHandler to be able to call the api, which works well.

However, when in my code I call the code to retrieve the Token, I get the exception + one unhandled exception:

[Inject]
public IAccessTokenProvider TokenProvider { get; set; }
...
var accessTokenResult = await TokenProvider.RequestAccessToken(); // <-- This throws exception

And additionally there is an unhandled exception:

Unhandled Exception:
System.Text.Json.JsonException: Invalid JSON
   at Microsoft.JSInterop.Infrastructure.DotNetDispatcher.EndInvokeJS(JSRuntime jsRuntime, String arguments)
   at Microsoft.AspNetCore.Components.WebAssembly.Services.DefaultWebAssemblyJSRuntime.<>c.<EndInvokeJS> b__7_0(String argsJson)
   at Microsoft.AspNetCore.Components.WebAssembly.Hosting.WebAssemblyCallQueue.Schedule[String](String state, Action`1 callback)
   at Microsoft.AspNetCore.Components.WebAssembly.Services.DefaultWebAssemblyJSRuntime.EndInvokeJS(String argsJson)
Uncaught Error: System.Text.Json.JsonException: Invalid JSONThe thread 0x1b444 has exited with code 0 (0x0).

I have tried in B2C to set the configuration, and I'm able to run the SignIn flow redirecting to jwt.ms passing the token and decoding it.

image.png

And I get the token decoded when redirecting directly to jwt.ms:

image.png


Original Comments

Feedback Bot on 11/12/2021, 00:15 AM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.


Original Solutions

(no solutions)

Activity

ghost added
Author: Migration Bot 🤖The issue was created by a issue mover bot. The author may not be the actual author.
on Jan 4, 2022
mkArtakMSFT

mkArtakMSFT commented on Jan 10, 2022

@mkArtakMSFT
Contributor

Thanks for contacting us. This is already fixed as part of #38962 / #39060.

TanayParikh

TanayParikh commented on Jan 10, 2022

@TanayParikh
Contributor

Please note, the fix for this issue should be available in the 6.0.2 patch release.

rmencia-isv

rmencia-isv commented on Jan 17, 2022

@rmencia-isv

Any estimates on this?
I've got this installed 6.0.200-preview.21617.4 and still not working

TanayParikh

TanayParikh commented on Jan 18, 2022

@TanayParikh
Contributor

The fix for the underlying System.Text.Json.JsonException: Invalid JSON exception was backported through #39075.

@rmencia-isv could you please try this out using the latest installer available at https://github.com/dotnet/installer. Please let us know if you're still having the issue with that, and please provide the exact version of the dotnet sdk being used, along side the exact exception message you're getting.

rmencia-isv

rmencia-isv commented on Jan 19, 2022

@rmencia-isv

I have installed the latest versionfrom the url you provided (dotnet-sdk-6.0.200-preview.22068.4-win-x64.exe) and I still get the same error in Blazor wasm client app.

Checking the list I have this one installed (6.0.200-preview.22068.4)
However, by looking at the call stack I see references to Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=6.0.1.0*

The error Message:
An exception occurred executing JS interop: The JSON value could not be converted to System.DateTimeOffset. Path: $.token.expires | LineNumber: 0 | BytePositionInLine: 73.. See InnerException for more details.

InnerException:
at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& state, Utf8JsonReader& reader, Exception ex)
at System.Text.Json.Serialization.JsonConverter1[[Microsoft.AspNetCore.Components.WebAssembly.Authentication.InternalAccessTokenResult, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=6.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state) at System.Text.Json.Serialization.JsonConverter1[[Microsoft.AspNetCore.Components.WebAssembly.Authentication.InternalAccessTokenResult, Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=6.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].ReadCoreAsObject(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
at System.Text.Json.JsonSerializer.ReadCore[Object](JsonConverter jsonConverter, Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
at System.Text.Json.JsonSerializer.Read[Object](Utf8JsonReader& reader, JsonTypeInfo jsonTypeInfo)
at System.Text.Json.JsonSerializer.Deserialize(Utf8JsonReader& reader, Type returnType, JsonSerializerOptions options)
at Microsoft.JSInterop.JSRuntime.EndInvokeJS(Int64 taskId, Boolean succeeded, Utf8JsonReader& jsonReader)'

TanayParikh

TanayParikh commented on Jan 19, 2022

@TanayParikh
Contributor

by looking at the call stack I see references to Microsoft.AspNetCore.Components.WebAssembly.Authentication, Version=6.0.1.0*

Ah that's definitely a bit strange. Could you please double check your csproj files to ensure you don't have any hardcoded versions which may be causing this issue? Also, if you could try adding the following to your nuget config:

<add key="dotnet6" value="https://dnceng.pkgs.visualstudio.com/public/_packaging/dotnet6/nuget/v3/index.json" />

Further details here.

rmencia-isv

rmencia-isv commented on Jan 19, 2022

@rmencia-isv

This is what I have in the project file.
I also added the nuget in VS to get the packages from that place and no new updates available, even pre-release for any of those packages.

image

image

TanayParikh

TanayParikh commented on Jan 19, 2022

@TanayParikh
Contributor

Hey @rmencia-isv, I took a look at the feeds and you're right the 6.0.2 (prerelease) packages aren't up yet. You can either try waiting for the official 6.0.2 release (mid February), or try the 7.0 alpha packages (not sure if they'll be updated yet though).

rmencia-isv

rmencia-isv commented on Jan 19, 2022

@rmencia-isv

Thanks Tanay for getting back to me. It's unfortunate that the packages are not uploaded yet, as I've seen a number of issues fixed.
I'll probably wait for the release next month, if the pre release packages are not updated.

53 remaining items

asieradzk

asieradzk commented on Jul 27, 2022

@asieradzk

Guys fix this?

ekuhlmann23

ekuhlmann23 commented on Aug 27, 2022

@ekuhlmann23

@kbeaugrand Could you provide more details on the workaround, please?

kbeaugrand

kbeaugrand commented on Aug 27, 2022

@kbeaugrand

@ekuhlmann23,

I simply moved the settings to specify the authentication response type to ``code```.

e.g:

   _ = builder.Services.AddOidcAuthentication(options =>
            {
                ....
                options.ProviderOptions.ResponseType = "code";
            });
modified the milestones: 7.0-rc1, 7.0-rc2 on Aug 29, 2022
Magik3a

Magik3a commented on Sep 1, 2022

@Magik3a

So, using new Blazor project with default B2C configuration is still erroring on the profile page.
So lets wait as the other guys on this :@
image
image

javiercn

javiercn commented on Sep 15, 2022

@javiercn
Member

We have made improvements to this scenario in .NET 7.0, the changes will be available in RC2.

It should be easier to diagnostic when there is an issue in the configuration of the app.

#43954

ghost locked as resolved and limited conversation to collaborators on Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

    Participants

    @syska@javiercn@Spaceman1861@kbeaugrand@Magik3a

    Issue actions

      Error retrieving the current token in a Blazor application after successful authentication in Azure B2C with Msal · Issue #39311 · dotnet/aspnetcore