我的JavaScript SignalR客户端通常连接到IdentityServer4的asp.net核心实现。
在客户端上,我看到:
Information: WebSocket connected to wss://localhost:5001/signal/satellite?
idsrv.session=4ec93d2f4c3b9a970ff82a537ae04d97&id=0eUBg2-xobp4CNttuIQJcg
但是,它也没有连接很多时间,并且错误显示位置0处的JSON中有意外令牌。
它似乎运行了好一阵然后又很差,即使我重新登录,它仍然会引发错误。在浏览器中,呼叫总是看起来大致像这样:
https://localhost:5001/signal/satellite?
idsrv.session=89364018a975e4fefff9ad0869b1ae09
我似乎不需要中间件来将querystring值移动到标头中,我仍然在onConnect期间通过其子声明使用户进入我的中心,但是,如果我确实在中间件之前放过app.UseAuthentication()并注意:
context.Request.QueryString.Value
成功与服务器握手后,我会看到:
idsrv.session=89364018a975e4fefff9ad0869b1ae09
但是如果没有,我会看到:
?CLIENT_ID =内部&REDIRECT_URI = HTTPS%3A%2F%2Flocalhost%3A5001%2Fsignin-OIDC&RESPONSE_TYPE =代码%20id_token&范围=的OpenID%20profile%20offline_access%20Harvey&response_mode = form_post&随机数= 636817245670169226.ZWM2OWQ0ZWEtOTQzMC00YTJlLWI4MzQtYmIxZDZjOWVlYjg5ZTA4NTU2M2QtNjczZi00MTlmLThjYmQtZWUzZTQ1ODMzNDQ0&状态= CfDJ8MCxOjJiJLdKqZrnPwOHDhqMnzWz6MqRb03SxToClqQ1F3n9g8yLdW683HRpZSHd-5wkN-6je4tHJkA8sc5i6YoKRxtMHwnWqxVW5-nXFaaH0TfOLUeqfxDzXLxnftmWFXLjK3Y7b6R2WzcDLEjChU1_Fk6X64SAHNRqeizGDPzRhxpV0U5w19Bbt7pUyRbYymn2WNedCS1F7g_wtwtJXDjCzWKBxqvPZ5Dtg99gxKkANalKYs7C4-fm7YdD0gFvsuV4CXsu0T06MjzID_zpA_F7TmSue4vGI- 0_qY55Swc5mbLWUwKHtj6ZTfOG4UmTEP_hbj2PO9w2oNg9TWqTPtDC3-qSl1fTUkY0EtCwbA7F&x-client-SKU = ID_NETSTANDARD1_4&x-client-ver = 5.2.0.0
所以我陷入困境,要么因为它自己成功而已,要么查询字符串从不使它移到中间件上进行迁移(这可能就是为什么它不能自己做到)
要持续工作,我缺少什么?
将Hybrid Client添加到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();
}
}
SignalR JavaScript客户端:
self.signalRConnection = new signalR.HubConnectionBuilder()
.withUrl("/signal/satellite?" + document.cookie).build();