Blazor Server App Failed to complete negotiation with the server: error - authentication

I build a blazor server application that supports Azure AD login and Azure B2C login. This works fine and I can decide what is displayed to the user using the authentication scheme.
If I login with Azure AD everything works fine. If I login with Azure B2C events (like onclick) are not raised.
After starting the application the user is sent to a login page. On this page he can decide to login with AD or B2C.
On this page I get the following error:
[2022-09-07T12:09:46.429Z] Error: Failed to start the connection: Error
[2022-09-07T12:09:46.430Z] Error: Error
Error: Cannot send data if the connection is not in the 'Connected' State.
at e.send (https://localhost:5001/_framework/blazor.server.js:1:51662)
at e.sendMessage (https://localhost:5001/_framework/blazor.server.js:1:22519)
at e.sendWithProtocol (https://localhost:5001/_framework/blazor.server.js:1:22581)
at https://localhost:5001/_framework/blazor.server.js:1:23244
at new Promise (<anonymous>)
at e.invoke (https://localhost:5001/_framework/blazor.server.js:1:23041)
at e.<anonymous> (https://localhost:5001/_framework/blazor.server.js:21:27207)
at https://localhost:5001/_framework/blazor.server.js:21:26507
at Object.next (https://localhost:5001/_framework/blazor.server.js:21:26612)
at https://localhost:5001/_framework/blazor.server.js:21:25524
After login with AD, the error is cleared and everything works fine. If I log in with B2C this error still occurs and it seems that the application is not longer connected to the server.
I think this is the reason why the application does fire events.

I was able to fix the problem.
Needed add the following:
endpoints.MapRazorPages().RequireAuthorization(
new AuthorizeAttribute
{
AuthenticationSchemes = $"{OpenIdConnectDefaults.AuthenticationScheme},{FischerLib.Extensions.Constants.B2CAuthenticationScheme}"
}
);
endpoints.MapBlazorHub().RequireAuthorization(
new AuthorizeAttribute
{
AuthenticationSchemes = $"{OpenIdConnectDefaults.AuthenticationScheme},{FischerLib.Extensions.Constants.B2CAuthenticationScheme}"
}
);

Related

Auth0 login locally

I have an application and an api, in terms of auth0, an static frontend and a expressjs backend, in production both are working. I setup Auth0 and users are able to login with Google accounts.
In production is working, but the problem I am facing is when I am working on my locally, when I run the application on my computer and I try to login I get the following error
Unhandled Runtime Error
Error: Request failed with status code 401
In the application setup I have
Allowed Callback URLs
https://www.myexample.com, http://localhost:3000, https://myexample.us.auth0.com/api/v2/
Allowed Logout URLs
https://www.myexample.com, http://localhost:3000
Allowed Web Origins
https://www.myexample.com, http://localhost:3000
and in the frontend
<Auth0Provider
domain="myexample.us.auth0.com"
clientId="XXXXXXXXXXX"
redirectUri={(process.browser) ? window && window.location.origin : 'http://localhost:3000'}
audience="https://myexample.us.auth0.com/api/v2/"
scope="read:current_user update:current_user_metadata"
>
If further information is needed, just let me know
I am not sure if I am missing some config or there is something I should change?

Blazor throwing error when using HttpContext

I want to get current windows user name when user opens the website. My application uses Blazor server-side. To get current username, I added:
In startup.cs:
services.AddHttpContextAccessor(); (under
ConfigureServices)
In razor page:
#inject IHttpContextAccessor httpContextAccessor
In razor page method:
string userName = httpContextAccessor.HttpContext.User.Identity.Name;
When I execute the code, the application throws an exception:
"An error has occurred. This application may no longer respond until reloaded."
I get this error only when I deploy on IIS. On local machine it works well.
I had a similar issue trying to access HttpContext for user information using Blazor. I found this here which was a life saver. HttpContext is NULL when running web app in IIS
All I had to do to solve the problem was to enable WebSockets in IIS.
Here are the steps: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/websockets?view=aspnetcore-3.1#iisiis-express-support
If you were to change that string from
string userName = httpContextAccessor.HttpContext.User.Identity.Name
to
string userName = httpContextAccessor?.HttpContext?.User?.Identity?.Name??"Anonymous"
then it would probably work.
It's because your IIS settings are allowing anonymous access and so your Identity object is null.
You should check for the inner exceptions behind the application crash but that will be your issue. You need to prevent anonymous access on IIS to get the Windows authentication passed through.
To manage IIS refer here https://stackoverflow.com/a/5458963/12285843

Docker API logs JWT issue

I am using Docker to run containers for an app that uses Angular, Identity Server, and an API(using a Compose.yml file to run them all together).
The API is responsible for returning database info, based on the user logged in through Identity Server. Locally, the API returns the user info after login without any issues. However, when I use Docker to run the app, the user can login but not have their db info returned to the angular page. I checked the logs for the API container, and am receiving the following errors:
fail: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[3]
fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
So it appears to me that there is an issue with the API receiving the token. I only see these error messages after I login through ID server. The ID server container logs do not display any errors before or after I login.
Can someone help me resolve this issue?

MobileFirst Obtain Access token and Login give no response after setting WL.App.setServerURL

As per my requirement, I am using WL.APP.setServerURL before every login request WLAuthorizationManager.loginof mobilefirst.
When I do first an valid/Invalid login attempt it works fine. As on next attempt I do WL.App.setServerURL, and then WLAuthorizationManager.login (login request) OR WLAuthorizationManager.obtainAccessToken (obtain access token request) don't respond anything. No success and no fail. Although non-secure adapter method works fine.
If I do WL.APP.setServerURL only once on application startup, everything works fine but in between call cause application failure.
let url = ... //fetcht url from adapter call
WL.App.setServerUrl(url, function (sucess) {
WLAuthorizationManager.login(securityCheckName, loginParam).then((data) => {
.... //After login code
}, (response) => {
//Login fail code
});
}, function (error) {
});
Flow :-
I call above code on click of login button.I call a method to get server url from adapter first then I set that url as server url.
When I do an invalid attempt of login it works find for first time, and on next call MFP login, it doesn't respond. Not success nor fail in application.
I am getting error in native logs android.
Android Error logs :-
E/SecurityCheckChallengeHandler(30199): SecurityCheckChallengeHandler.submitChallengeAnswer in SecurityCheckChallengeHandler.java:51 :: submitAnswer has been called for unknown request
.
MobileFirst server version :- 8.0.0.00-20170911-123510
MobileFirst Cordova(plugin) version :- 8.0.2017102403
The WL.App.setServerUrl API is meant to be used sparingly whenever the app needs to connect to a server other than the one defined in mfpclient.properties. If you want to keep the server URL constant, you should just put it up in the mfpclient.properties.
Having said that, the behaviour you mention is not expected - can you update your question with a code snippet of your app performing login & setServerUrl

Box API token request enterprise configuration issue

I am making a call to https://api.box.com/oauth2/token to get a token for a user.
I get a 400 with the following error:
Cannot obtain token based on the enterprise configuration for your app
I don't see this error message defined anywhere. Any ideas what I'm doing wrong?
The issue here was that I was requesting a token for a non app user and my application was configured to allow access to app users only.