簡體   English   中英

SignalR 身份驗證始終為 null

[英]SignalR Authentication Identity is always null

我有一個 WPF 應用程序,我正在連接到 SignalR API。但是,我的身份存在一些問題。 當我實際調用 API 端點 GET、POST、PUT 或 DELETE 時,身份已正確填充(所有聲明都在那里)。 當我連接到 SignalR 時,身份聲明不存在。

這是我注冊我的 Jwt 令牌的地方

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .AddJwtBearer(options => {
            options.TokenValidationParameters = new TokenValidationParameters {
                ValidateIssuer = true,
                ValidateAudience = true,
                ValidateLifetime = true,
                ValidateIssuerSigningKey = true,
                ValidIssuer = configuration["Jwt:Issuer"],
                ValidAudience = configuration["Jwt:Audience"],
                IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["Jwt:Key"]))
            };
            options.Authority = configuration["Jwt:Authority"];
            options.RequireHttpsMetadata = false;
            options.SaveToken = true;
            options.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("/hubs")))
                    {
                        // Read the token out of the query string
                        context.Token = accessToken;
                    }

                    return Task.CompletedTask;
                },
            };
    });

這是我注冊連接的地方

  _connection = new HubConnectionBuilder()
        .ConfigureLogging(logBuilder =>
        {
            logBuilder.AddConsole();
            logBuilder.AddDebug();
        })
        .WithUrl($"{url}/hubs", options =>
        {
            options.AccessTokenProvider = _userService.GetToken;//returns Task.FromResult(userToken)
        })
        .WithAutomaticReconnect(new[]
        {
            TimeSpan.Zero,
            TimeSpan.FromSeconds(2),
            TimeSpan.FromSeconds(10),
            TimeSpan.FromSeconds(30),
            TimeSpan.FromSeconds(60),
            TimeSpan.FromSeconds(120)
        })
        .Build();

我的Jwt object在配置

"jwt":{
    "Key":"some random generated key",
    "Issuer":"https://localhost:5001/",
    "Audience":"https://localhost:5001/",
    "Authority":"https://localhost:5001/"
  },

你能解釋一下我在這里做錯了什么嗎?

我實際上找到了我自己問題的答案。

我變了

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)

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

就是這樣。 希望這可以幫助別人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM