【问题标题】:User.Identity.Name is null for Blazor Wasm hosted in asp.net core对于托管在 asp.net 核心中的 Blazor Wasm,User.Identity.Name 为空
【发布时间】:2021-11-19 16:28:30
【问题描述】:

对于托管在 asp.net 核心中的 Blazor Wasm,User.Identity.Name 为空。 没有诸如姓名或电子邮件之类的声明。我使用的是 Visual Studio 的默认模板。

services.AddIdentityServer().AddApiAuthorization<ApplicationUser, ApplicationDbContext>();

services.AddAuthentication().AddIdentityServerJwt();

我错过了什么吗?

如果您需要更多信息,请告诉我。

【问题讨论】:

    标签: c# asp.net identityserver4


    【解决方案1】:

    您的代码没有任何实际意义。您不会在 blazor 应用程序中托管 IdentityServer,而是有一个单独的专用服务来实现 OpenIDConnect,并且可以对用户进行身份验证并提供令牌。

    在 blazor 应用程序中,您通常使用这种类型的框架来配置身份验证:

    services.AddAuthentication(options =>
    {
        options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
    }).AddCookie(opt =>
       //Add configuration here
    {
    }).AddOpenIdConnect(options =>
    {
       //Add configuration here
    });
    

    详情请看这篇文章: How do I implement Blazor authentication with OpenID Connect?

    【讨论】:

    • Blazor Wasm 托管在 ASP.NET Core Web API 内部,并且该服务器内部具有 IdentityServer。 (我不确定,但我在 msdn 上阅读的资源暗示了这一点)
    • 另外我没有你提供的骨架。
    • 我仍然会将 IdentityServer 放在单独的 ASP.NET 核心服务中,因为根据我的经验,推理和排除故障会困难得多。我真的很喜欢关注点分离是这里的一个重要规则。因为作为开发人员,您确实需要了解正在发生的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 2022-11-10
    相关资源
    最近更新 更多