OData / WebApi

OData Web API: A server library built upon ODataLib and WebApi
https://docs.microsoft.com/odata
Other
855 stars 473 forks source link

Exception when handle odata batch request when is has cookie #1620

Open ptrushin opened 6 years ago

ptrushin commented 6 years ago

I think it is because in file https://github.com/OData/WebApi/blob/master/src/Microsoft.AspNetCore.OData/Batch/ODataBatchReaderExtensions.cs

in line 227 context.Request.Cookies = originalContext.Request.Cookies; add a "Cookie" key in context.Request.Headers and

in line 230 context.Request.Headers.Add(header); add it again

Assemblies affected

Microsoft.AspNetCore.OData/7.0.1

Reproduce steps

Odata batch request with cookie

Expected result

Ok

Actual result

Exception

Additional detail

System.ArgumentException: An item with the same key has already been added. Key: Cookie at System.Collections.Generic.Dictionary2.TryInsert(TKey key, TValue value, InsertionBehavior behavior) at System.Collections.Generic.Dictionary2.Add(TKey key, TValue value) at Microsoft.AspNetCore.Http.HeaderDictionary.Add(KeyValuePair2 item) at Microsoft.AspNet.OData.Batch.ODataBatchReaderExtensions.CreateHttpContext(HttpContext originalContext) at Microsoft.AspNet.OData.Batch.ODataBatchReaderExtensions.ReadOperationInternalAsync(ODataBatchReader reader, HttpContext originalContext, Guid batchId, Nullable1 changeSetId, CancellationToken cancellationToken, Boolean bufferContentStream) at Microsoft.AspNet.OData.Batch.DefaultODataBatchHandler.ParseBatchRequestsAsync(HttpContext context) at Microsoft.AspNet.OData.Batch.DefaultODataBatchHandler.ProcessBatchAsync(HttpContext context, RequestDelegate nextHandler) at Microsoft.AspNet.OData.Batch.ODataBatchMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context) at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

Madhust commented 6 years ago

I have faced this exact issue. Any way to disable cookies from adding in the request??

itrenkler commented 6 years ago

Unfortunately I've got the same issue. Has anybody found a solution or workaround?

emumanu commented 5 years ago

Hello, I was stuck in this problem too.

The problem went away when I registered the authentication stuff before all OData stuff.

Farthom commented 5 years ago

It aint pretty, but I worked around this issue by pulling a fast one on the "UseODataBatching" middleware.

app.Use(async (context, next) => { context.Request.Headers["CookieBackup"] = context.Request.Headers["Cookie"]; context.Request.Headers.Remove("Cookie"); await next(); });

app.UseODataBatching();

app.Use(async (context, next) => { context.Request.Headers["Cookie"] = context.Request.Headers["CookieBackup"]; context.Request.Headers.Remove("CookieBackup"); await next(); });

SmithWilson commented 4 years ago

Hello, I was stuck in this problem too.

The problem went away when I registered the authentication stuff before all OData stuff.

Thanks, mate, it helped me.

tarakpatel1985 commented 4 years ago

Hello, I was stuck in this problem too.

The problem went away when I registered the authentication stuff before all OData stuff.

Can you please elaborate how you did. it is working in local but in server it gives this error. in start up I have below.

ssviat commented 3 years ago

Looks like it was fixed as part of this pr #2379

groogiam commented 3 years ago

Looks like it was fixed as part of this pr #2379

Yes I am using 8.0.0-beta and this is fixed.