如何在Identity项目之外访问UserManager



我有三个项目,称为Identity、SPA和API。

Identity项目使用IdentityServer4处理身份验证和授权。SPA是一个单页应用程序,它使用Identity进行身份验证,并使用Identity提供的令牌与API通信。API使用Identity根据SPA传递的令牌提供授权。

我想使用UserManager访问Identity中的ApplicationUser数据。Identity数据库与API共享,但我正在尝试使用Identity API来访问其他用户数据。如果我将Identity移到它自己的数据库中,除了设置中的数据库引用之外,应用程序不会发生更改。

所以我的问题是:如何访问UserManager来更新API中的附加用户数据?

我使用以下代码将用户管理器添加到Identity之外的附加项目中:

var userStoreType = typeof(UserStore<,,,>).MakeGenericType(typeof(User), typeof(IdentityRole<Guid>), typeof(MyDbContext), typeof(Guid));
var roleStoreType = typeof(RoleStore<,,>).MakeGenericType(typeof(IdentityRole<Guid>), typeof(MyDbContext), typeof(Guid));
services.TryAddScoped(typeof(IUserStore<>).MakeGenericType(typeof(User)), userStoreType);
services.TryAddScoped(typeof(IRoleStore<>).MakeGenericType(typeof(IdentityRole<Guid>)), roleStoreType);
services.TryAddScoped<IPasswordHasher<User>, PasswordHasher<User>>();
services.TryAddScoped<ILookupNormalizer, UpperInvariantLookupNormalizer>();
services.TryAddScoped<IdentityErrorDescriber>();
services.TryAddScoped<UserManager<User>>();

最新更新