UserManager.GenerateEmailConfirmationTokenAsync 生成的令牌不适用于其他域

UserManager.GenerateEmailConfirmationTokenAsync generated token does not work from other domain

我正在尝试创建一个身份提供者,用户可以在其中登录到应用程序(然后被重定向),并管理他们的帐户凭据,并提供不记名令牌。单独运行管理应用程序逻辑的 Blazor WASM Web 应用程序,还有一个管理面板,管理员可以在其中创建用户、重置密码、管理 roles/permissions 等。此管理面板调用 API.

现在,当我在此管理面板中创建用户时,会调用 API,它会发送一封确认电子邮件,其中包含 URL,其中包含由 [=40 生成的电子邮件确认令牌=] 身份的 UserManager.GenerateEmailConfirmationTokenAsync。令牌是由 API (localhost:5002) 调用 class 库生成的,但是 URL 确认是到 https://localhost:5000/Account/ConfirmEmail?用户 ID=xxxxxxxxxxxxxxxxxx&token=yyyyyyyyyyyyyyyyyyyy。当我单击此 link 时,我收到消息:VerifyUserTokenAsync() failed with purpose: EmailConfirmation for user xxxxxxx-xxxxxx-xxxxxx-xxxx-xxxxxxxxxxxxxx. 以及错误消息:“InvalidToken”。

身份提供者运行ASP.NET Identity,IdentityServer4,并在ASP.NET Core 3.1 中编写。该应用程序是用 Blazor(即 netstandard2.1)编写的,然后调用 API(ASP.NET Core 3.1),后者调用 class 库(ASP.NET Core 3.1 ).所有项目都使用相同的 DatabaseContexts。

我已经tried设置

services.AddDataProtection()
    .PersistKeysToFileSystem(new DirectoryInfo(Configuration["AppSettings:keydir"]))
    .SetApplicationName(Configuration["AppSettings:keyname"]);

在身份提供者和 API 中,但运气不好。我也试过设置:

services.AddDataProtection(options =>
    {
        options.ApplicationDiscriminator = "myapp";
    })

类似于此问题:https://github.com/dotnet/aspnetcore/issues/22081

我想这可能是因为我正在调用额外的 class 库,我是否必须在那里设置一些 DataProtection 设置?

我想通了。不同的项目不是问题,数据保护也不是问题。原来我在创建用户之前生成了一个确认令牌。这样生成的令牌是基于一个空的安全戳。然后在另一个项目中,usermanager 根据非 null securitystamp 的令牌验证令牌,因为现在已经创建了用户。这当然会产生无效的令牌。