SignalR ws authentication failure on full framework with Windows Authentication for alpha
Hi,
SignalR doesnt work with Windows authentication on top of the full framework 4.6.2. I'm running on Windows 10 anniversary edition.
I have created a simple sample to reproduce this https://github.com/douglaswaights/AspNetCoreSignalrWinAuth
The sample is based on the angular spa template.
When the hub tries to connect from the spa with typescript client you get Transports.js:28 WebSocket connection to 'ws://localhost:65394/MyHub?id=b78ec818-fabb-4b75-9c3b-4676bd4c0a8a' failed: HTTP Authentication failed; no valid credentials available
Change the launch settings to disable windows auth and enable anonymous and the sample works again as expected.
How to make signalr work with Windows authentication?
Thanks Doug
Can you try a different browser and see if it works? Chrome did not support Windows Authentication and I don't know if this has changed.
Thanks for the reply!
I have tried with Edge and Firefox and can confirm that it works fine in those browsers. The sample doesn't seem to load in IE 11 but i don't think that's anything to do with signalr.
I had no idea Windows Auth didnt work in Chrome for signalr over websockets. This is a bit of a blow given the enterprise nature of our app and popularity of Chrome. I'm a bit surprised given how many on-prem services i log into by providing windows credentials when challenged by Chrome.
Can you point me at a sample or some code where we configure the signalr transports in the client so can avoid websockets in this situation? I guess in previous versions it has just fallen back to one of these...
I wonder if you any other insight / thoughts on how to tackle this for enterprise style apps reliant on windows auth?
Thanks Doug
I just wrote this function you might find helpful. It simulates transport fallback
function startConnection(url, configureConnection) {
return function start(transport) {
console.log(`Starting connection using ${signalR.TransportType[transport]} transport`)
var connection = new signalR.HubConnection(url, {transport: transport});
if (configureConnection && typeof configureConnection === 'function') {
configureConnection(connection);
}
return connection.start()
.then(function() {
return connection;
})
.catch(function(error) {
console.log(`Cannot start the connection use ${signalR.TransportType[transport]} transport. ${error.message}`);
if (transport !== signalR.TransportType.LongPolling) {
return start(transport + 1);
}
return Promise.reject(error);
});
}(signalR.TransportType.WebSockets);
}
Here is a sample usage: https://github.com/aspnet/SignalR-samples/pull/8
@douglaswaights try adding two options in chrome://policy for policies with names: AuthNegotiateDelegateWhitelist AuthServerWhitelist
This enables windows authentication in Chrome. I didn't tried it with WebSockets, but for standard websites this works perfect.
Thanks for the suggestion @Misiu and for the code samples and fallback helper @moozzyk . Looking at this now :-)
@Misiu Sadly, the chrome://policy settings do not work in Chrome for WebSockets. I have also been struggling with this, and I just found a bug ticket on Chromium.org concerning this that has been open since Oct 15, 2014. https://bugs.chromium.org/p/chromium/issues/detail?id=423609. I would surmise this is a general problem with WebKit as Opera does not work either, I have not tested Safari. I was able to get Authentication working for both SSE and Long Polling in Chrome, so @moozzyk transport fallback is probably the best way to go here.
edit: I was getting some odd behavior when the function falls back to SSE, whenever the browser prompts for credentials the connection would connect but blowup with a 404 to a Post request, so I made some slight changes to @mozzyk's function, so that it retries the same transport on a Not Found error, it also rejects on an Unauthorized response rather than attempting to fallback.
private startConnection(url: string, configureConnection: (hubConnection : HubConnection) => void): Promise<HubConnection> {
return function start(transport : TransportType) : Promise<HubConnection> {
console.log(Starting connection using ${TransportType[transport]} transport.`);
var connection = new HubConnection(url, { transport: transport });
if (configureConnection && typeof configureConnection === 'function') {
configureConnection(connection);
}
return connection.start()
.then(() => {
console.log('Connected.');
return connection;
})
.catch(err => {
console.log(`Cannot start the connection using ${TransportType[transport]} transport. Error: ${err ? err.message : 'undefined'}`);
if (err !== undefined && err.message === "Unauthorized") {
return Promise.reject(err);
}
if (err !== undefined && err.message === "Not Found") {
return start(transport);
}
if (transport !== TransportType.LongPolling) {
return start(transport + 1);
}
return Promise.reject(err);
});
}(TransportType.WebSockets);
}`
@dhhunter - can you tell me more details about the 404 you were getting with SSE? There is an issue that looks similar (SSE returning 404) which I was not able to repro. I would like to understand what is happening.
It appears to be another quirk in Chrome, the SSE connection is connecting (I would guess with cached credentials); however, Chrome displays the login prompt anyway and things go haywire. I have not been able to reproduce in other browsers (since WebSockets work properly in FireFox & Edge). Opera while having the same problem with WebSockets as Chrome, does not appear to have the same quirky behavior with the SSE transport.
Starting connection using ServerSentEvents transport. vendor-bundle.js:27869 Information: Normalizing '/chat' to 'http://localhost:54601/chat' vendor-bundle.js:27869 Information: SSE connected to http://localhost:54601/chat?id=af7e2c8f-4e65-42e2-aaeb-4de2a965d651 => Chrome displays Login Prompt here, then
vendor-bundle.js:1395 Warning: a promise was rejected with a non-error: [object Undefined] at EventSource.c.onerror (http://localhost:54601/scripts/vendor-bundle.js:27869:14037)...
User submits his credentials then: vendor-bundle.js:27869 POST http://localhost:54601/chat?id=af7e2c8f-4e65-42e2-aaeb-4de2a965d651 404 (Not Found) chatClient.ts:86 Cannot start the connection using ServerSentEvents transport. Error: Not Found
Here I detect the 404 error and reconnect using SSE again, this time Chrome does not display the login prompt and everything connects properly.
Request details:
1) General: Request URL:http://localhost:54601/chat Request Method:OPTIONS Status Code:200 OK Remote Address:[::1]:54601 Referrer Policy:no-referrer-when-downgrade
Response Headers: Allow:GET,POST,OPTIONS Content-Length:125 Content-Type:application/json Date:Tue, 10 Oct 2017 21:19:26 GMT Persistent-Auth:true Server:Kestrel X-Powered-By:ASP.NET X-SourceFiles:=?UTF-8?B?QzpcVXNlcnNcaGRodW50ZXJcc291cmNlXHJlcG9zXFNpZ25hbFJDb3JlXEF1cmVsaWFDaGF0XGNoYXQ=?=
Request Headers: Accept:/ Accept-Encoding:gzip, deflate, br Accept-Language:en-US,en;q=0.8 Connection:keep-alive Host:localhost:54601 Origin:http://localhost:54601 Referer:http://localhost:54601/ User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36 X-Requested-With:XMLHttpRequest
Response: {"connectionId":"f730d427-f793-437f-b0c7-7b9f5f8833f4","availableTransports":["WebSockets","ServerSentEvents","LongPolling"]}
General: Request URL:http://localhost:54601/chat?id=f730d427-f793-437f-b0c7-7b9f5f8833f4 Request Method:GET Status Code:200 OK Remote Address:[::1]:54601 Referrer Policy:no-referrer-when-downgrade
Response Headers: Cache-Control:no-cache Content-Encoding:identity Content-Type:text/event-stream Date:Tue, 10 Oct 2017 21:19:26 GMT Persistent-Auth:true Server:Kestrel Transfer-Encoding:chunked X-Powered-By:ASP.NET X-SourceFiles:=?UTF-8?B?QzpcVXNlcnNcaGRodW50ZXJcc291cmNlXHJlcG9zXFNpZ25hbFJDb3JlXEF1cmVsaWFDaGF0XGNoYXQ=?=
Request Headers: Accept:text/event-stream Accept-Encoding:gzip, deflate, br Accept-Language:en-US,en;q=0.8 Cache-Control:no-cache Connection:keep-alive Host:localhost:54601 Referer:http://localhost:54601/ User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36
Query String Parameters: id:f730d427-f793-437f-b0c7-7b9f5f8833f4
General: Request URL:http://localhost:54601/chat?id=af7e2c8f-4e65-42e2-aaeb-4de2a965d651 Request Method:POST Status Code:404 Not Found Remote Address:[::1]:54601 Referrer Policy:no-referrer-when-downgrade
Response Headers: Date:Tue, 10 Oct 2017 17:56:48 GMT Persistent-Auth:true Server:Kestrel Transfer-Encoding:chunked X-Powered-By:ASP.NET X-SourceFiles:=?UTF-8?B?QzpcVXNlcnNcaGRodW50ZXJcc291cmNlXHJlcG9zXFNpZ25hbFJDb3JlXEF1cmVsaWFDaGF0XGNoYXQ=?=
Request Headers: Accept:/ Accept-Encoding:gzip, deflate, br Accept-Language:en-US,en;q=0.8 Authorization:Negotiate TlRMTVNTUA... Connection:keep-alive Content-Length:20 Content-Type:text/plain;charset=UTF-8 Host:localhost:54601 Origin:http://localhost:54601 Referer:http://localhost:54601/ User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36 X-Requested-With:XMLHttpRequest
Query String Parameters: id:af7e2c8f-4e65-42e2-aaeb-4de2a965d651
Request Payload: {"protocol":"json"}
Response: No Connection with that ID
Connecting to SSE directly in Chrome (rather than attempting WebSockets first and downgrading) appears to work as intended.
Please let me know if you require anything else to help you figure out the issue.
Thanks for the detailed explanation and request dump.
It seems that the login prompt appears when the client is trying to send the POST request. The problem is that the server is waiting 5 seconds for the {"protocol":"json"}� after receiving the OPTIONS request. In case of SSE the {"protocol":"json"}� is supposed to be sent with the first POST request which is getting delayed by the login prompt. If the server does not receive the {"protocol":"json"}� within 5 seconds it will remove the connection and any further requests having the connection id for the removed connection will be rejected with 404.
Unfortunately, I don't know why Chrome shows the login prompt and how to prevent it.
Is there a plan to make SignalR fallback by default if transport is not supported or not working?
Yep: #1373 - There will be limited transport fallback support (in that if the transport fails during the initial handshake we will fallback). Also, we're looking to do a better job at detecting issues that would cause a transport to fail and simply skipping it.