2

我已经设置了一个Blazor WebAssembly ASP.NET Core hosted应用程序并集成了身份验证系统以及AuthenticationProvider客户端的自定义。在服务器端项目中,我在控制器中调用:

[HttpGet]
public async Task<ActionResult> GetObjects()
{
   var id = User.FindFirstValue(ClaimTypes.NameIdentifier); // <-- returns always null
}

并且id变量总是返回null。

用户来自 aspnetcoreuser 表(aspnetcore 身份) 在表数据中,我可以看到,当我使用 SQLite 时,id 不为空,并且用户已成功添加到aspnetuser表中。

一些 SDK 版本:

安装的 .NET Core SDK:3.1.301 安装的 .NET Core 运行时:Microsoft.AspNetCore.All 2.1.19 Microsoft.AspNetCore.App 2.1.19Microsoft.AspNetCore.App 3.1.5 Microsoft.NETCore.App 2.1.19 Microsoft.NETCore。应用程序 3.1.5 Microsoft.WindowsDesktop.App 3.1.5

我还看到了一些解决方案,这些解决方案"windowsAuthentication": true, "anonymousAuthentication": false,可能launchSettings.json会导致这种情况,但是随着更改它并没有为我解决。

这里有人可以帮助我吗?

4

1 回答 1

2

您需要UserIdClaimType通过将此代码添加到服务器应用程序上进行设置Startup.ConfigureServices

services.Configure<IdentityOptions>(options => 
    options.ClaimsIdentity.UserIdClaimType = ClaimTypes.NameIdentifier);

在这里阅读更多

于 2020-07-09T00:53:09.003 回答