Skip to content

@microsoft/signalr (JavaScript) accessTokenFactory not providing access_token query param #48926

@dapperdandev

Description

@dapperdandev

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Building and starting a connection with accessTokenFactory doesn't add access_token to the query parameters. The resulting request path ends up being /{hub}/negotiate?negotiateVersion=1. It should be /{hub}/negotiate?negotiateVersion=1&access_token=token given the example below.

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/7.0.7/signalr.min.js"></script>
    <!-- Using this CDN is just for repro simplicity. I'm installing @microsoft/signalr and importing in ts in my real project  -->
    <script>
        const connection = new signalR.HubConnectionBuilder().withUrl("/notifications", {
            accessTokenFactory: () => "token" // should append &access_token=token to the query string
        }).build();

        connection.start().then(() => console.log('started'));
    </script>

</body>
</html>
builder.Services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer(options =>
  {
      options.Authority = Environment.GetEnvironmentVariable("AUTH0_AUTHORITY_URL")
      options.Events = new JwtBearerEvents
      {
          OnMessageReceived = context =>
          {
              var accessToken = context.Request.Query["access_token"]; // no value
              var path = context.HttpContext.Request.Path;
              if (!string.IsNullOrEmpty(accessToken) &&
                  (path.StartsWithSegments("/notifications")))
              {
                  context.Token = accessToken;
              }
              return Task.CompletedTask;
          }
      };
  });

https://learn.microsoft.com/en-us/aspnet/core/signalr/authn-and-authz?view=aspnetcore-7.0#built-in-jwt-authentication

Expected Behavior

Providing the accessTokenFactory should append access_token={token} to the query params on connection.start().then(() => ...)

Steps To Reproduce

Save this html as an index.html and open it in your browser.

<title>Document</title> <script src="https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/7.0.7/signalr.min.js"></script> <script> const connection = new signalR.HubConnectionBuilder().withUrl("/notifications", { accessTokenFactory: () => "token" // should append &access_token=token to the query string }).build();
    connection.start().then(() => console.log('started'));
</script>

Exceptions (if any)

N/A

.NET Version

7.0.203

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    ✔️ Resolution: AnsweredResolved because the question asked by the original author has been answered.Status: Resolvedarea-signalrIncludes: SignalR clients and servers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions