Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

How to pass Azure B2C access tokens in Blazor Server

$
0
0

I have been working for like few days on trying to figure out a way to fetch the access token in blazor server application, but so far it didnt worked out.

this is what i have in program.cs

builder.Services.AddMicrosoftIdentityWebAppAuthentication(builder.Configuration, Constants.AzureAdB2C);builder.Services.AddInMemoryTokenCaches();`

also when i try to fetch the token using ITokenAcquisition in razor page, i see another error although user is already signed in:

MSAL.NetCore.4.60.3.0.MsalUiRequiredException:ErrorCode: user_nullMicrosoft.Identity.Client.MsalUiRequiredException: No account or login hint was passed to the AcquireTokenSilent call.

also another problem is that i have to pass the scope for, my enterprise portal which i couldn't figure how to do in Blazor server, although below code i can see for Blazor WASM:

  builder.Services.AddMsalAuthentication(options =>  { builder.Configuration.Bind("AzureAdB2C",    options.ProviderOptions.Authentication);    options.ProviderOptions.DefaultAccessTokenScopes.Add("enterpriseportalscopeurl");     options.ProviderOptions.LoginMode = "redirect"; });

i also tried this

[Inject]public ITokenAcquisition TokenAcquisitionService { get; set; }`string token = await TokenAcquisitionService.GetAccessTokenForUserAsync("scopeurl", authenticationScheme: "OpenIdConnect")

but it resulted in error: below is some exception related to that

Microsoft.Identity.Web.TokenAcquisition: Error: False MSAL 4.60.3.0 MSAL.NetCore .NET 8.0.2 Microsoft Windows 10.0.19044 [2024-05-09 14:20:01Z - 61481e5d-6a0d-47c8-854b-744efd5bd249] Exception type: Microsoft.Identity.Client.MsalUiRequiredException, ErrorCode: user_nullHTTP StatusCode 0CorrelationId 61481e5d-6a0d-47c8-854b-744efd5bd249To see full exception details, enable PII Logging. See https://aka.ms/msal-net-logging   at Microsoft.Identity.Client.Internal.Requests.Silent.SilentRequest.ExecuteAsync(CancellationToken cancellationToken)   at Microsoft.Identity.Client.Internal.Requests.RequestBase.<>c__DisplayClass11_1.<<RunAsync>b__1>d.MoveNext()--- End of stack trace from previous location ---   at Microsoft.Identity.Client.Utils.StopwatchService.MeasureCodeBlockAsync(Func`1 codeBlock)   at Microsoft.Identity.Client.Internal.Requests.RequestBase.RunAsync(CancellationToken cancellationToken)Exception thrown: 'Microsoft.Identity.Client.MsalUiRequiredException' in Microsoft.Identity.Client.dllException thrown: 'Microsoft.Identity.Client.MsalUiRequiredException' in System.Private.CoreLib.dllException thrown: 'Microsoft.Identity.Client.MsalUiRequiredException' in System.Private.CoreLib.dllException thrown: 'Microsoft.Identity.Client.MsalUiRequiredException' in System.Private.CoreLib.dllMicrosoft.Identity.Web.TokenAcquisition: Information: [MsIdWeb] An error occured during token acquisition: No account or login hint was passed to the AcquireTokenSilent call. MSAL.NetCore.4.60.3.0.MsalUiRequiredException:    ErrorCode: user_nullMicrosoft.Identity.Client.MsalUiRequiredException: No account or login hint was passed to the AcquireTokenSilent call.    at Microsoft.Identity.Client.Internal.Requests.Silent.SilentRequest.ExecuteAsync(CancellationToken cancellationToken)   at Microsoft.Identity.Client.Internal.Requests.RequestBase.<>c__DisplayClass11_1.<<RunAsync>b__1>d.MoveNext()--- End of stack trace from previous location ---   at Microsoft.Identity.Client.Utils.StopwatchService.MeasureCodeBlockAsync(Func`1 codeBlock)   at Microsoft.Identity.Client.Internal.Requests.RequestBase.RunAsync(CancellationToken cancellationToken)   at Microsoft.Identity.Client.ApiConfig.Executors.ClientApplicationBaseExecutor.ExecuteAsync(AcquireTokenCommonParameters commonParameters, AcquireTokenSilentParameters silentParameters, CancellationToken cancellationToken)   at Microsoft.Identity.Web.TokenAcquisition.GetAuthenticationResultForWebAppWithAccountFromCacheAsync(IConfidentialClientApplication application, ClaimsPrincipal claimsPrincipal, IEnumerable`1 scopes, String tenantId, MergedOptions mergedOptions, String userFlow, TokenAcquisitionOptions tokenAcquisitionOptions)   at Microsoft.Identity.Web.TokenAcquisition.GetAuthenticationResultForUserAsync(IEnumerable`1 scopes, String authenticationScheme, String tenantId, String userFlow, ClaimsPrincipal user, TokenAcquisitionOptions tokenAcquisitionOptions)    StatusCode: 0     ResponseBody:      Headers: Exception thrown: 'Microsoft.Identity.Web.MicrosoftIdentityWebChallengeUserException' in Microsoft.Identity.Web.TokenAcquisition.dllException thrown: 'Microsoft.Identity.Web.MicrosoftIdentityWebChallengeUserException' in System.Private.CoreLib.dllException thrown: 'Microsoft.Identity.Web.MicrosoftIdentityWebChallengeUserException' in System.Private.CoreLib.dllException thrown: 'Microsoft.Identity.Web.MicrosoftIdentityWebChallengeUserException' in System.Private.CoreLib.dll

It will be a great help if anyone already solved this problem.

I tried with token acquisition and it throw the msal ui exception.Also i tried catching token during app load, but it works only when app is build, if i leave my app open, it doesn't go to builder code.`


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>