-
Notifications
You must be signed in to change notification settings - Fork 10.4k
[blazor-wasm] Pass access token as query string when running SignalR in the browser #20115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 9 commits
dc1aa2a
c14b0bf
05cf101
5ca0f1b
a03cb87
dd7a83c
348d79b
e0af487
e70e52d
6272fa3
4a56599
f83f5ef
9908f8a
7d931f6
f9341c8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -6,6 +6,7 @@ | |||
using System.IO.Pipelines; | ||||
using System.Net.WebSockets; | ||||
using System.Runtime.InteropServices; | ||||
using System.Text.Encodings.Web; | ||||
using System.Threading; | ||||
using System.Threading.Tasks; | ||||
using Microsoft.AspNetCore.Connections; | ||||
|
@@ -23,6 +24,7 @@ internal partial class WebSocketsTransport : ITransport | |||
private readonly ILogger _logger; | ||||
private readonly TimeSpan _closeTimeout; | ||||
private volatile bool _aborted; | ||||
private bool _isRunningInBrowser; | ||||
|
||||
private IDuplexPipe _transport; | ||||
|
||||
|
@@ -87,6 +89,8 @@ public WebSocketsTransport(HttpConnectionOptions httpConnectionOptions, ILoggerF | |||
|
||||
// Ignore the HttpConnectionOptions access token provider. We were given an updated delegate from the HttpConnection. | ||||
_accessTokenProvider = accessTokenProvider; | ||||
|
||||
_isRunningInBrowser = Utils.IsRunningInBrowser(); | ||||
} | ||||
|
||||
public async Task StartAsync(Uri url, TransferFormat transferFormat, CancellationToken cancellationToken = default) | ||||
|
@@ -113,7 +117,17 @@ public async Task StartAsync(Uri url, TransferFormat transferFormat, Cancellatio | |||
var accessToken = await _accessTokenProvider(); | ||||
if (!string.IsNullOrEmpty(accessToken)) | ||||
{ | ||||
_webSocket.Options.SetRequestHeader("Authorization", $"Bearer {accessToken}"); | ||||
// We can't use request headers in the browser, so instead append the token as a query string in that case | ||||
if (_isRunningInBrowser) | ||||
{ | ||||
var accessTokenEncoded = UrlEncoder.Default.Encode(accessToken); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice to test this code path. Maybe we can make There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Writing a test for this is probably something I'd need help with - do you know of a test I can use as an example of some similar scenario? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably something similar to
|
||||
accessTokenEncoded = "access_token=" + accessTokenEncoded; | ||||
resolvedUrl = Utils.AppendQueryString(resolvedUrl, accessTokenEncoded); | ||||
} | ||||
else | ||||
{ | ||||
_webSocket.Options.SetRequestHeader("Authorization", $"Bearer {accessToken}"); | ||||
} | ||||
} | ||||
} | ||||
|
||||
|
Uh oh!
There was an error while loading. Please reload this page.