dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.

Home Page:https://asp.net

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Blazor server azure b2c redirect issue

setin1219 opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Hi team,

Application: Blazor Server
Render Mode: Interactive
Sign-in through Azure B2C

I have below simple snippet which I am trying to get the access token

[AuthorizeForScopes(ScopeKeySection = "AzureAdB2C:Scopes")]
private async Task GetTokenService()
{
try
{
var accessToken = await AuthTokenService.GetTokenAsync();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
// Process the exception from a user challenge
ConsentHandler.HandleException(ex);
}
}

public async Task GetTokenAsync()
{
var accessToken = await _tokenAcquisition.GetAccessTokenForUserAsync(new[] { _myAppScope });
return accessToken;
}
but it always fails inside ConsentHandler and i see the message "This localhost page can’t be found " in browser

i have inspected the code which ran inside consent handler and i see that the redirects fails

string text = $"{BaseUri}/{"MicrosoftIdentity/Account/Challenge?redirectUri="}{value}&{"scope"}={string.Join(" ", first)}&{"loginHint"}={User.GetLoginHint()}&{"domainHint"}={User.GetDomainHint()}&{"claims"}={claims}&{"policy"}={userflow}";
if (IsBlazorServer)
{
NavigationManager.NavigateTo(text, forceLoad: true);
return;
}
HttpContext httpContext2 = _httpContextAccessor.HttpContext;
lock (httpContext2)
{
httpContext2.Response.Redirect(text);
}

Expected Behavior

token to be fetch since it is working in .net 7 code

Steps To Reproduce

No response

Exceptions (if any)

Microsoft.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_null
Microsoft.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.<b__1>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.Identity.Client.Utils.StopwatchService.MeasureCodeBlockAsync(Func1 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, IEnumerable1 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.dll

.NET Version

8.0.200

Anything else?

No response

issue raised under microsoft.identity.web for better visibility

AzureAD/microsoft-identity-web#2831

hence closing this.