带有AzureAD身份验证的blazor,Context.Identity.User.Name为null [英] Blazor with AzureAD Auth, Context.Identity.User.Name is null

查看:53
本文介绍了带有AzureAD身份验证的blazor,Context.Identity.User.Name为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只有经过身份验证的用户才能按预期访问该应用程序.我需要能够通过Signalr跟踪用户.例如,如果我运行ChatHub类型的服务,我希望人们能够使用应该自动设置的AzureAD用户名聊天,而不是让人们设置自己的用户名.

Only authenticated users can access the application as expected. I need to be able to track users via signalr. For example, if I run a ChatHub type of service, I'd like people to be able to chat using their AzureAD username which should be set automatically and not let people set their own usernames.

中心始终显示Context.Identity.User.Name为空.

The hub always shows Context.Identity.User.Name is 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?

Any idea if here is a way to preserve identity information and pass to SignalR?

推荐答案

检查您的 JWT令牌并检查其声明.您可以将其粘贴在 http://jwt.ms/上以对其进行解码.然后,查找返回的引用用户名的声明(在我的情况下为 preferred_username ).

Inspect your JWT token and check its claims. You can past it on http://jwt.ms/ to decode it. Then, look for the claims that are being returned that references the user name (in my case it is preferred_username).

然后,您可以使用以下代码更改 Identity.Name 的默认映射:

Then you can change the default mapping of the Identity.Name using this code:

services.Configure<OpenIdConnectOptions>(AzureADDefaults.AuthenticationScheme, options =>
{
    options.TokenValidationParameters.NameClaimType = "<claim_name_that_returns_username>";
});

这篇关于带有AzureAD身份验证的blazor,Context.Identity.User.Name为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆