I'm developing a C# web app (edit: Blazor WASM) where users are logged in via Azure AD.
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(...)
It works well – the browsers send an HTTP authorization header with a bearer token that the middleware in my app interprets. I am now setting up a three-legged OAuth connection to a third-party server where my client app will make changes on the user's behalf. I am again using Microsoft middleware (Microsoft.AspNetCore.Authentication.OAuth
):
services.AddAuthentication(...)
.AddCookie()
.AddOAuth(...);
It starts out well, and the browser is redirected to the third-party server's OAuth authorization URL. However, the CORS flow kicks in and the browsers send a preflight OPTIONS request to the server with headers like this:
OPTIONS /oauth2/auth?client_id=123abc[...]
Accept: */*
Access-Control-Request-Method: GET
Access-Control-Request-Headers: authorization
Origin: https://localhost:7260
It seems like the browsers (I've tried both Firefox and Chrome) want to send "my" authorization header along to the third-party server. Unfortunately, the third-party server isn't able to respond properly to these requests. It just responds with a redirect to /home
without any of the required headers, so the browsers throw errors and the flow stops.
I can't control the third-party server so I would like to avoid the preflight request. It doesn't trigger when I'm testing without being logged in to my client app, but in production I need the users to be logged in.
Is there some way I can prevent the browsers from trying to send the authorization header to the redirected page? I have played around with OnRedirectToAuthorizationEndpoint
, but I didn't get anywhere.
Alternatively, can I set it up so that "my" user sessions are handled via a cookie instead of using the authorization header?
And finally, is there some good documentation of all this anywhere? I can always read the dotnet sources, but doing so is not very efficient.