只有经过身份验证的用户才能按预期访问应用程序。我需要能够通过信号器跟踪用户。例如,如果我运行 ChatHub 类型的服务,我希望人们能够使用他们的 AzureAD 用户名进行聊天,该用户名应该自动设置,而不是让人们设置自己的用户名。
集线器始终显示 Context.Identity.User.Name 为 null。
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => Configuration.Bind("AzureAd", options));
services.AddTransient<HubConnectionBuilder>();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub<App>(selector: "app");
endpoints.MapFallbackToPage("/_Host");
endpoints.MapHub<SomeHub>("/SomeHub");
});
知道这里是否有一种保存身份信息并传递给 SignalR 的方法吗?
检查你的JWT token
并检查其声明。你可以把它粘贴到http://jwt.ms/ http://jwt.ms/对其进行解码。然后,查找返回的引用用户名的声明(在我的例子中是preferred_username
).
然后您可以更改默认映射Identity.Name
使用此代码:
services.Configure<OpenIdConnectOptions>(AzureADDefaults.AuthenticationScheme, options =>
{
options.TokenValidationParameters.NameClaimType = "<claim_name_that_returns_username>";
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)