asp.net-core - 将 SignalR 添加到现有 IdentityServer4 授权

标签 asp.net-core identityserver4 asp.net-core-signalr

我有一个 Asp.Net core 2.2 IdentityServer4 应用程序,其工作系统支持声明等。我正在向其中添加 SignalR,并希望使用 [Authenitcation] header 并有权访问我的 Controller 拥有的相同声明。

我发现了几篇关于将 SignalR 与 IdentityServer4 集成的文章,但我无法判断哪些内容与我已经在做的事情有重叠,以及添加对 SignalR 的支持需要哪些内容。我只需要将具体的SignalR路由告知IdentityServer即可进行授权吗?

这是一篇详尽的文章,其中包含 GitHub 上的大量示例: https://mikebridge.github.io/articles/identityserver4-signalr/ https://github.com/mikebridge/IdentityServer4SignalR

最佳答案

我最终重新设计了 IdentityServer4 的用法来创建 jwtbearer token 并使用 HybridAndClientCredentials,并且在我的信号器 session 启动事件中拾取了用户声明。

将混合客户端添加到 IdentityServer4:

        new Client
        {
            ClientId = "mvc",
            ClientName = "MVC Client",
            AllowedGrantTypes = GrantTypes.HybridAndClientCredentials,

            ClientSecrets = 
            {
                new Secret("secret".Sha256())
            },

            RedirectUris = { "http://localhost:5002/signin-oidc" },
            PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" },

            AllowedScopes = 
            {
                IdentityServerConstants.StandardScopes.OpenId,
                IdentityServerConstants.StandardScopes.Profile,
                "api1"
            },
            AllowOfflineAccess = true
        }

然后在 mvc 客户端启动时:

 ServiceCollection.AddAuthentication(options =>
            {
                options.DefaultScheme = "Cookies";
                options.DefaultChallengeScheme = "oidc";
            })
            .AddCookie("Cookies")
            .AddOpenIdConnect("oidc", options =>
            {
                options.SignInScheme = "Cookies";

                options.Authority = "https://localhost:5001";
                options.RequireHttpsMetadata = false;
                options.ClientSecret = "secret";
                options.ClientId = "mvc";

                options.ResponseType = "code id_token";
                options.SaveTokens = true;
                options.GetClaimsFromUserInfoEndpoint = true;

                options.Scope.Add("offline_access");
                options.Scope.Add("api1");
                options.ClaimActions.MapJsonKey("website", "website"); 
            });

这是根据示例建模的:Quickstart5_HybridAndApi

在服务器上的 SignalR 中:

        [Authorize]
        public class SignalRSignalHub : Hub
        {
            public override Task OnConnectedAsync()
            {
                var context = Context.GetHttpContext();
                return base.OnConnectedAsync();
            }
        }

关于asp.net-core - 将 SignalR 添加到现有 IdentityServer4 授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53864445/

相关文章:

c# - Asp.Net Core 中 RouteValueDictionary 的路径

c# - .Net Core Integration TestServer 与 Generic IHostBuilder

javascript - oidc-client-js 没有从 Identity Server 4 正确获取声明

identityserver4 - CSP扫描仪: Wildcard Directive alert for OpenID Connect session management endpoint

手机闪屏出现时 Signalr 最佳实践

c# - 使用 .NET-Standard 2.0 项目构建 ASP.NET-Core 3.1 会导致冲突的 Microsoft.AspNetCore.Mvc.Analyzers 程序集

asp.net-core - Asp.net 5 DNX 和 DNU 无法在 Windows Server 2008 R2 中工作

IdentityServer4 中的 Azure AD 声明

asp.net-core-signalr - .Net Core 3.1 SignalR 客户端 - 如何将 JWT token 字符串添加到 SignalR 连接配置?

asp.net-core - signalR 核心打开连接两次