Repository navigation
Description
From @dgweldon on February 15, 2018 22:28
I have an interesting problem related to OpenID Connect Authentication in ASP .NET Core. We have an Identity Server 3 OIDC implementation that uses a client certificate authentication provider. When migrating to ASP .NET Core, we had no problem integrating with our existing OIDC server. At least when launched from a browser (Edge, Chrome).
However, we have a small ClickOnce application (using the full .NET framework) that also authenticates and makes calls through the ASP. It does this by using a HttpClient with a certificate selected by the user. This ClickOnce application fails during authentication. The code is pretty simple.
using (WebRequestHandler certHandler = new WebRequestHandler
{
CookieContainer = new CookieContainer(),
ClientCertificateOptions = ClientCertificateOption.Manual,
UseDefaultCredentials = true,
AllowAutoRedirect = true
})
{
// Init
certHandler.ClientCertificates.Add(certificate);
using (HttpClient client = new HttpClient(certHandler) { BaseAddress = baseUri })
{
HttpResponseMessage response = a_client.GetAsync(ssoLoginUri).GetAwaiter().GetResult();
response.EnsureSuccessStatusCode();
// Process self submitting form responses as long as needed
do
{
response = ProcessFormAutoSubmit(a_client, response);
}
while (response != null);
}
}After moving to ASP .NET Core, this code no longer works. The issue is that when MVC redirects to Identity Server, it includes 2 cookies (.ASPNetCore.Correlation.OpenIdConnect and .AspNetCore.OpenIdConnect.Nonce). Those cookies never make it into my CookieContainer (other cookies from Identity Server are handled fine). When the signin-oidc callback is made, MVC fails with the error: "Correlation failed: .AspNetCore.Correlation.OpenIdConnect.9N18bzJ1hcpG5YjRMw_QgUJ_PnapfVx2lBmdvb8Q_zA' cookie not found.". Makes sense. But why is the cookie not added?
After playing around, I was able to get around this by handling each redirect and manually adding any cookies that had a path (both of which the 2 missing cookies had). The code now looks like this:
...
HttpResponseMessage response = a_client.GetAsync(ssoLoginUri).GetAwaiter().GetResult();
Regex cookiePathRegex = new Regex(@"path=([^;]*);?");
do
{
// Ideally we would just use automatic redirects in the HttpClient, but we need to intercept cookies for special processing
if (response.StatusCode == HttpStatusCode.Redirect)
{
Uri requestUri = response.Headers.Location;
if (!requestUri.IsAbsoluteUri)
{
requestUri = UriUtil.Combine(response.RequestMessage.RequestUri.GetLeftPart(UriPartial.Authority), response.Headers.Location.OriginalString);
}
response = a_client.GetAsync(requestUri).GetAwaiter().GetResult();
IEnumerable<string> responseCookies = null;
if (response.Headers.TryGetValues("set-cookie", out responseCookies))
{
// Cookies that have a path aren't currently supported with HttpClient
// Such cookies are used with the new ASP .NET Core OpenId Connect implementation
// Just manually add these cookies
foreach (string sCookie in responseCookies)
{
Match pathMatch = cookiePathRegex.Match(sCookie);
if (pathMatch.Success)
{
a_webRequestHandler.CookieContainer.SetCookies(
UriUtil.Combine(requestUri.GetLeftPart(UriPartial.Authority), pathMatch.Groups[1].Value),
cookiePathRegex.Replace(sCookie, String.Empty));
}
}
}
}
else
{
response.EnsureSuccessStatusCode();
response = ProcessFormAutoSubmit(a_client, response);
}
}
while (response != null);This works, but seems like a hack. Any idea why these cookies aren't handled by HttpClient?
I realize this may not be directly be an ASP .NET Core issue, but wanted to start here since it seems to be related to cookies from ASP .NET Core.
Thanks,
--David
Copied from original issue: dotnet/aspnetcore#2880
Activity
Tratcher commentedon Feb 27, 2018
From @Eilon on February 16, 2018 5:30
Hi, a few things I'm wondering:
Tratcher commentedon Feb 27, 2018
From @dgweldon on February 16, 2018 13:12
I apologize for leaving out those details. That's how I actually found this issue. In Fiddler, I can see that the 2 cookies mentioned are not passed to signin-oidc. Also, we are using ASP.NET Core 2.0.5. And on the ClickOnce application we are using 5.2.4 of Microsoft.AspNet.WebApi.Client for HttpClient.
Tratcher commentedon Feb 27, 2018
From @Eilon on February 18, 2018 23:50
@dgweldon thanks for the info. Is there any difference in the cookies sent by the server to the client, comparing ASP.NET's cookies to ASP.NET Core?
Ultimately I don't think HttpClient particularly cares who sent it the cookies, so presumably there's a difference in what it got from the initial request, and thus isn't sending the cookie to the subsequent request.
Tratcher commentedon Feb 27, 2018
From @dgweldon on February 20, 2018 16:34
Just to be clear, I'm talking about 2 clients (web browser and ClickOnce app using HttpContext) going through the same ASP .NET Core server. There is no ASP .NET server in play in this problem scenario.
But the cookies sent to each client look very similar.
Here are the ones sent to the web browser:
And then the ones sent to the ClickOnce application:
Tratcher commentedon Feb 27, 2018
From @danroth27 on February 20, 2018 17:45
@Tratcher
Tratcher commentedon Feb 27, 2018
This is a client bug. https://github.com/dotnet/corefx/blob/ac58f7e99e7745ba9ab85fe7b47c348260b6ef26/src/System.Net.Http/src/uap/System/Net/cookie.cs#L574
The client requires the cookie path to be less specific than the current request (e.g. request is /foo/bar and cookie can be /foo) but we are setting a path that's more specific (e.g. request is / and we set /signin-oidc). There's no provision for this behavior in the spec (https://tools.ietf.org/html/rfc6265.html#section-5.2.4) and browsers handle it fine.
I did find one difference between net461 and netcoreapp2. For net461 if you have one cookie rejected due to the path then you still get the other cookie. With netcoreapp2 if you have any cookies rejected then they are all rejected.
I'll redirect this to the corefx team.
Repro code:
Tratcher commentedon Feb 27, 2018
On a different note, the OIDC protocol is not designed for programmatic usage like this. You'd be better off using a bearer token model.
davidsh commentedon Feb 27, 2018
Currently .NET Core and .NET Framework do NOT implement RFC 6265.
Related: #19746
caesar-chen commentedon Apr 26, 2018
I think it's duplicated of #22372.
[-]HttpClient does not handle cookies with a non-matching path[/-][+][Cookie] HttpClient does not handle cookies with a non-matching path[/+]caesar-chen commentedon May 11, 2018
Tracked in #26141.
Test for #27520
Test for #27520
Adapt RFC 6265 in path handling for cookies (#39250)
Adapt RFC 6265 in path handling for cookies (dotnet#39250)