将 SignalR 添加到现有的 IdentityServer4 授权
Adding SignalR to existing IdentityServer4 Authorization
我有一个 Asp.Net 核心 2.2 IdentityServer4 应用程序,其工作系统支持声明等。我正在向其中添加 SignalR 并希望使用 [Authenitcation] header 并有权访问我的控制器也有同样的说法。
我找到了几篇关于将 SignalR 与 IdentityServer4 集成的文章,但我无法确定哪些内容与我已经在做的事情重叠,以及添加对 SignalR 的支持需要哪些内容。是否只需要通知 IdentityServer 具体的 SignalR 路由即可授权?
这是一篇详尽的文章,其中包含有关 GitHub 的大量示例:
https://mikebridge.github.io/articles/identityserver4-signalr/
https://github.com/mikebridge/IdentityServer4SignalR
我最终重新设计了我的 IdentityServer4 用法以创建一个 jwtbearer 令牌并使用 HybridAndClientCredentials 并且在我的 signalr 会话启动事件中获取了用户声明。
将混合客户端添加到 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 核心 2.2 IdentityServer4 应用程序,其工作系统支持声明等。我正在向其中添加 SignalR 并希望使用 [Authenitcation] header 并有权访问我的控制器也有同样的说法。
我找到了几篇关于将 SignalR 与 IdentityServer4 集成的文章,但我无法确定哪些内容与我已经在做的事情重叠,以及添加对 SignalR 的支持需要哪些内容。是否只需要通知 IdentityServer 具体的 SignalR 路由即可授权?
这是一篇详尽的文章,其中包含有关 GitHub 的大量示例: https://mikebridge.github.io/articles/identityserver4-signalr/ https://github.com/mikebridge/IdentityServer4SignalR
我最终重新设计了我的 IdentityServer4 用法以创建一个 jwtbearer 令牌并使用 HybridAndClientCredentials 并且在我的 signalr 会话启动事件中获取了用户声明。
将混合客户端添加到 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();
}
}