简体   繁体   中英

SignalR core JWT claims

cfg.Events = new JwtBearerEvents
                    {
                        OnMessageReceived = context =>
                        {
                            var accessToken = context.Request.Query["access_token"];

                            // If the request is for our hub...
                            var path = context.HttpContext.Request.Path;
                            if (!string.IsNullOrEmpty(accessToken) &&
                                (path.StartsWithSegments("/sas")))
                            {
                                // Read the token out of the query string
                                context.Token = accessToken;
                            }

                            return Task.CompletedTask;
                        }
                    };

in my hub i tried to get claims like that

Context.User.Claims

but theyre empty

so is there any option to get my JWT claims in my signalr hub?

I had a similar issue and I've solved it by replacing this:

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)

with this:

services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})

For some reason, I had to explicitly define the DefaultAuthenticateScheme . I still don't know why the DefaultAuthenticateScheme didn't fallback automatically to the DefaultScheme as it should according to the documentation. If I find out the reason, I'll update the answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM