aspnetcore: Bad Request - Request Too Long - IIS Server
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
I cloned the repository and started working with the BlazorWebappOidc project. I configured the Entra ID settings, such as the client ID and tenant ID, using the details from my Entra ID tenant account. After running the application locally on the Kestrel server with the HTTPS profile, the Blazor application launched successfully. The interface loaded in the browser, and I was able to log in using my Entra ID account. After a successful callback to my Blazor application URL, I was redirected and received the expected details (Such as Username, email address, etc).
However, when I deployed the same application on an IIS server, I encountered an issue. While the Blazor application opened, when I attempted to log in to my Microsoft Entra ID account, I received a Bad Request - Request Too Long (HTTP Error 400. The size of the request headers is too long) error after the callback.
Expected Behavior
After a successful callback to my Blazor application URL, I was redirected and received the expected details (Such as Username, email address, etc).
Steps To Reproduce
-
Go to repository and clone BlazorWebappOidc and Open the application in Visual Studio.
-
Provide the needed configuration like Tenant ID, Client ID, etc for your Microsoft Azure Entra ID.
-
Add an IIS Settings to launchsettings.json to run the application on IIS locally then Run
"iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:46294", "sslPort": 44381 } },"profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } }
-
Click on login and it will redirect you to the authentication flow then provide the Credentials for login (Entra ID Username and password).
-
After the successful callback you get back to the Blazor Application and then you will see the Error
Bad Request - Request Too Long (HTTP Error 400. The size of the request headers is too long.)
Exceptions (if any)
No response
.NET Version
DOTNET 8
Anything else?
cc: @guardrex https://github.com/dotnet/blazor-samples/issues/344
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 1
- Comments: 20 (12 by maintainers)
Given that this issue has arisen and concern that other devs are going to face it in the future with users who have many AD groups/AD built-in Admin Roles, I now plan to modify the approach described by the article.
Let’s obtain AD groups/AD built-in Admin Roles via Graph SDK/API separately for authenticating users on sign in. It will eliminate this problem for everyone forever. I already have a separate issue to include an add-on section for Graph SDK/API setup anyway.
@kuldeepcis-lab … The soonest that I can reach it is next week (if no fires come up between now and then) due to a couple of factors … 🦖🧠🔥😆 … I’m tired and need a few days off. We also have the Labor Day holiday on Monday. If you can wait until next week, I’ll have a new Graph-based approach for you.
@halter73 @MackinnonBuck @javiercn … If everyone is in agreement with my plan, this PU issue can be closed in favor of the existing docs issues …
dotnet/AspNetCore.Docs#33099)dotnet/AspNetCore.Docs#33450)@halter73 … I’ll ping u for review when the docs PR goes up.
@kuldeepcis-lab What did you try setting
MaxRequestBytesto? Did you confirm changing this setting allowed you to manually send a request with more than 16 KiB of request headers?Even Kestrel has a limit on the total size of request headers. If Kestrel can handle request headers of this size without custom configuration, IIS should also be able to with the right configuration.
The default
MaxRequestHeadersTotalSizefor Kestrel is 32 KiB while IIS’sMaxRequestBytes(which is nearly equivalent except that the IIS limit also counts the size of the request line in addition to the headers) is 16 KiB, but this can be increased all the way to 16 MiB.There’s also the
MaxFieldLength, but that shouldn’t come into play considering the cookies are chunked at 4 KiB per header, and theMaxFieldLengthlimit is 16 KiB per header.However, I think @javiercn is on the right track. You probably don’t want to take the performance hit of sending such large cookies every request anyway. Your best bet is likely to first inspect which claims are getting stored in the cookie and try clearing any unnecessary claims from the cookie using
OpenIdConnectOptions.ClaimActions.DeleteClaim. https://learn.microsoft.com/en-us/aspnet/core/security/authentication/social/additional-claims?view=aspnetcore-8.0#remove-claim-actions-and-claimshttps://learn.microsoft.com/troubleshoot/developer/webapps/iis/www-administration-management/http-bad-request-response-kerberos has some similar guidance to https://learn.microsoft.com/troubleshoot/developer/webapps/iis/iisadmin-service-inetinfo/httpsys-registry-windows, but it’s first suggestion is to “Decrease the number of Active Directory groups”. This is for Kerberos rather than OIDC, but the principle is the same. If you try to encode too many claims into a header (in this case the “Cookie” header), you’ll eventually run into limits.
@guardrex by adding the MaxFieldLength and MaxRequestBytes parameter to registry. it is still not working
At a glance, https://github.com/guardrex/BlazorWebAppMSIdentityWeb/ looks pretty good to me. You might be able to leverage
AddMicrosoftIdentityUIto get rid ofLoginLogoutEndpointRouteBuilderExtensions, but it’s nice that the latter doesn’t rely on MVC, so I understand leaving it there. I might do the same in the “official” version we use for the template.Instead of putting the Microsoft Graph logic inside of
OnPersistingAsync, you could do the same thing inOpenIdConnectOptions.Events.OnTokenValidated. It might also be worth checking if you can get the same claims you can from Microsoft Graph if you configureOpenIdConnectOptions.GetClaimsFromUserInfoEndpoint = true. If we did that, we’d need to updateCookieOidcRefresherto also query theuserinfo_endpoint. I agree that usingAddMicrosoftIdentityWebAppis best if you’re connecting to Entra ID. And I’m not sure how much the datauserinfo_endpointoverlaps with the Microsoft Graph data.I’m also not sure that
JwtSecurityTokenHandler.DefaultMapInboundClaims = falsedoes anything anymore, sinceJwtSecurityTokenhas been deprecated and is no longer used unless you configureUseSecurityTokenValidators = true. https://learn.microsoft.com/en-us/dotnet/core/compatibility/aspnet-core/8.0/securitytoken-eventsYou should be able to achieve the same result with
JsonWebTokenby callingJsonWebTokenHandler.DefaultInboundClaimTypeMap.Clear(). Or if you don’t want to rely on mutating static state, you can still configureOpenIdConnectOptions.MapInboundClaims = falseas follows:It’s also probably better to use an array of DownstreamApi scopes rather than splitting a single string with
builder.Configuration.GetValue<string>("DownstreamApi:Scopes")?.Split(' ').AddMicrosoftGraph(builder.Configuration.GetSection("DownstreamApi"))should already be reading the scopes as an array. I’m not sure ifEnableTokenAcquisitionToCallDownstreamApineeds the initial scopes, but if it does, you should be able to manually read them as an array without splitting usingbuilder.Configuration.GetValue<string[]>("DownstreamApi:Scopes").Also, where is
HandleSameSiteCookieCompatibility()coming from? And what does it do exactly?@kuldeepcis-lab thanks for contacting us.
The request that fails is the one that happens after the logging, isn’t it? Seems that too much information might be stored in the auth cookie. (There are 5 chunks at 4Kb per chunk)
So I suspect IIS has a 16KB header limit.
In that situation, you need to either configure a higher limit (not sure if possible) or reduce the amount of information that you put on the cookie.
You can do so by hooking on to the Oidc call and customizing the claims principal https://learn.microsoft.com/en-us/aspnet/core/security/authentication/claims?view=aspnetcore-8.0
Already tried, but Not Worked.