【问题标题】:Failed SignalR accentuations using Identity Server 4使用 Identity Server 4 失败的 SignalR 重音
【发布时间】:2020-12-27 17:50:55
【问题描述】:

我设置了 Identity Server 4、Asp.Net Core SignalR Hub 和 JavaScript 客户端。 当我尝试连接到 SignalR 集线器时,“negotiateVersion:1”正确通过,但对集线器的请求根本没有通过。 Chrome 中的错误是“HTTP 身份验证失败;没有可用的有效凭据”和 FireFox 中的 401 状态代码。 id 和访问令牌出现在查询中。我尝试了不同的示例,但没有得到想要的结果。

版本: 身份服务器 4 - .Net Core 2.1

  • IdentityServer4.AspNetIdentity(2.5.0)

Asp.Net Core SignalR Hub - .Net Core 3.1

  • IdentityServer4.AccessTokenValidation(3.0.1)
  • Microsoft.AspNetCore.Authentication.JwtBearer(3.1.7)

JavaScript 客户端

  • signalr.min.js v4.2.2+97478eb6

这是我在 Identity Server 中的配置: 配置文件

ClientId = "my.client",
AllowedGrantTypes = GrantTypes.Implicit,
AllowedScopes =
{
    IdentityServerConstants.StandardScopes.OpenId,
    IdentityServerConstants.StandardScopes.Profile,
    "api1"
},
Claims = { },
RedirectUris = {
    "https://mySite/popupFrame.html",
    "https://mySite/silentFrame.html",
},
PostLogoutRedirectUris = {"https://mySite/logoutFrame.html" },
RequireConsent = false,
AllowAccessTokensViaBrowser = true,
AccessTokenLifetime = (int)TimeSpan.FromMinutes(10).TotalSeconds,                

ConfigureServices 方法中的Startup.cs

services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryPersistedGrants()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())
.AddAspNetIdentity<ApplicationUser>()
.AddResourceOwnerValidator<ResourceOwnerValidator>()
.AddProfileService<ProfileService>();

services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, AppClaimsPrincipalFactory>();

这是我在 Asp.Net SignalR 中的配置: ConfigureServices 方法中的 Startup.cs

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    .AddIdentityServerAuthentication(options =>
    {
        options.Authority = "https://localhost:44341";
        options.RequireHttpsMetadata = false;
        options.ApiName = "api1"; 
        options.JwtBearerEvents = new JwtBearerEvents
        {
            OnMessageReceived = context =>
            {
                StringValues queryAccessToken = context.Request.Query["access_token"];

                PathString path = context.HttpContext.Request.Path;
                if (path.StartsWithSegments("/brokerhub") && !string.IsNullOrEmpty(queryAccessToken)) 
                {
                    context.Token = queryAccessToken;
                }
                
                return Task.CompletedTask;
            },
        };
    });

services.AddCors();
services.AddSignalR();
services.AddControllers();

Configure 方法中的Startup.cs

app.UseRouting();

app.UseCors(builder =>
{
    builder.WithOrigins("https://mySite", "http://localhost")
        .AllowAnyHeader()
        .WithMethods("GET", "POST")
        .AllowCredentials();
});

app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
    endpoints.MapHub<BrokerHub>("/brokerhub", options =>
    {
        options.Transports = HttpTransportType.WebSockets;
    });
});

集线器控制器

[Authorize]
public class BrokerHub : Hub

最后是 JS 客户端

client = new signalR.HubConnectionBuilder()
        .withUrl('https://hub.com/brokerhub/', {
            accessTokenFactory: () => {
                return user.access_token;
            }
        })
        .build();

谁能指出我在配置中缺少什么?

【问题讨论】:

  • 嗨,@Antonio Iliev,它适用于 Edge 还是 IE?试试AllowedGrantTypes = GrantTypes.HybridAndClientCredentials。你可以查看Adding SignalR to existing IdentityServer4 Authorization
  • 嗨@MichaelWang。感谢你的快速回复。我在 Edge 中有相同的错误消息,而 IE 完全不起作用:)。我尝试使用混合授权类型,但不幸的是,身份服务器抛出“客户端的无效授权类型:隐式”并拒绝对用户进行身份验证。感谢您提供链接,但 ASP.Net Core 3.1 更改了服务的 API,不再包含 AddOpenIdConnect 方法。

标签: authentication identityserver4 jwt-auth asp.net-core-signalr


【解决方案1】:

也许这个问题是相关的 - https://github.com/IdentityServer/IdentityServer4/issues/2349#issuecomment-394099795

您能否尝试使用 options.TokenRetriever 代替上面链接中显示的 OnMessageReceived 并介绍 CustomTokenRetriever?

【讨论】:

    猜你喜欢
    • 2020-05-01
    • 2018-07-23
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    • 2018-01-19
    • 2021-04-14
    • 1970-01-01
    相关资源
    最近更新 更多