在 Identity Asp.net core 3 MVC 中创建服务 IUserStore 时出错

2024-02-06

Update

请参阅下面的更新。

我问过一个之前的 stackoverflow 问题 https://stackoverflow.com/questions/58654400/store-does-not-implement-iuserrolestoretuser-usermanagertuser-getuserrolesto最近,但在实施以下两个地方建议的解决方案后,我遇到了一个问题ASP.NET Core 中的身份模型自定义 https://learn.microsoft.com/en-us/aspnet/core/security/authentication/customize-identity-model?view=aspnetcore-3.0 and ASP.NET Core Identity 的自定义存储提供程序 https://learn.microsoft.com/en-us/aspnet/core/security/authentication/identity-custom-storage-providers?view=aspnetcore-3.0。我只是无法让解决方案发挥作用。我已经实现了扩展IdentityUser按照建议并延长IdentityRole。我已经实施了IUserPasswordStore and IUserRoleStore and IUserStore for UserStore我已经实施了IRoleStore for RoleStore。我还实现了新的 dbContextApplicationDbContext它实现了IdentityDbContext。构造函数不接受参数存在问题,因此我实现了一个新的构造函数。我不确定这是否正确。

然而,这似乎无关紧要,因为我在调用时在 Program.cs 的 Main 方法中收到错误

CreateHostBuilder(args).Build().Run(); 

而且误差很大。我把它放在最后了。在线搜索错误的部分并没有提供关于我正在做的事情的任何想法,并且由于我是 ASP.NET Core 3 和 Identity 的新手,所以我被难住了。

这是我到目前为止的代码。

配置服务 method

public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();
        services.AddMvc(options =>
        {
            var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
            options.Filters.Add(new AuthorizeFilter(policy));
        options.EnableEndpointRouting = false;
        });

        services.AddDbContext<EntitiesModel>(options => options.UseSqlServer(
            Configuration["Data:ConnectionStrings:XXXXDbConnection"]));
        services.AddIdentity<UserViewModel,RoleViewModel>().AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders().AddRoles<RoleViewModel>(); ;
        services.AddTransient<IUserStore<UserViewModel>, UserStore>();
        services.AddTransient<IRoleStore<RoleViewModel>, RoleStore>();
        services.ConfigureApplicationCookie(options =>
        {
            options.LoginPath = "/Login";
            options.LogoutPath = "/Logout";
        });
    }

应用程序数据库上下文- 这些都在同一个命名空间中

public class IdentityDbContext
    : IdentityDbContext<IdentityUser, IdentityRole, string>
{
    public IdentityDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
    {
    }
}

public class IdentityDbContext<TUser>
    : IdentityDbContext<TUser, IdentityRole, string>
    where TUser : IdentityUser
{
    public IdentityDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
    {
    }
}

// Uses the built-in Identity types except with custom User and Role types
// The key type is defined by TKey
public class IdentityDbContext<TUser, TRole, TKey> : IdentityDbContext<
    TUser, TRole, TKey, IdentityUserClaim<TKey>, IdentityUserRole<TKey>,
    IdentityUserLogin<TKey>, IdentityRoleClaim<TKey>, IdentityUserToken<TKey>>
    where TUser : IdentityUser<TKey>
    where TRole : IdentityRole<TKey>
    where TKey : IEquatable<TKey>
{
    private DbContextOptions<ApplicationDbContext> options;
    //private string nameOrConnectionString;

    public IdentityDbContext(DbContextOptions<ApplicationDbContext> options)
    {
        this.options = options;
    }

}

public abstract class IdentityDbContext<
        TUser, TRole, TKey, TUserClaim, TUserRole, TUserLogin, TRoleClaim, TUserToken>
    : IdentityUserContext<TUser, TKey>
    where TUser : IdentityUser<TKey>
    where TRole : IdentityRole<TKey>
    where TKey : IEquatable<TKey>
    where TUserRole : IdentityUserRole<TKey>
{

}
public class ApplicationDbContext : IdentityDbContext<UserViewModel,RoleViewModel,int>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<UserViewModel>(b =>
        {               
            b.HasMany(e => e.UserRoles)
                .WithOne()
                .HasForeignKey(ur => ur.Id)
                .IsRequired();
        });
        modelBuilder.Entity<UserViewModel>(b =>
        {
            b.ToTable("T_CustomerContacts");
        });
        modelBuilder.Entity<RoleViewModel>(b =>
        {
            b.ToTable("T_LoginRoles");
        });
        modelBuilder.Entity<UserRoleViewModel>(b =>
        {
            b.ToTable("T_CustomerContactRole");
        });         
        modelBuilder.Entity<RoleViewModel>(b =>
        {
            b.HasMany(e => e.UserRoles)
                .WithOne(e => e.Role)
                .HasForeignKey(ur => ur.RoleId)
                .IsRequired();
        });
    }
}

用户视图模型

public class UserViewModel : IdentityUser<int>
{

用户角色视图模型

public class UserRoleViewModel : IdentityUserRole<int>
{

角色视图模型

public class RoleViewModel : IdentityRole<int>
{   

角色库

public class RoleStore : IRoleStore<RoleViewModel>
{

用户存储

public class UserStore : IUserStore<UserViewModel>, IUserPasswordStore<UserViewModel>, IUserRoleStore<UserRoleViewModel>
{

我必须补充一点,这是我尝试过的最终配置。我尝试了很多组合和解决方案。 部分错误如下

启动应用程序时发生错误。 AggregateException:无法构造某些服务(验证服务描述符'ServiceType时出错:Microsoft.AspNetCore.Identity.IUserStore1[TrussCorp.CustomerPortal.Models.ViewModel.Identity.UserViewModel] Lifetime: Scoped ImplementationType: Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore4[TrussCorp.CustomerPortal.Models.ViewModel.Identity.UserViewModel,TrussCorp.CustomerPortal.Models.ViewModel.Identity.RoleViewModel,TrussCorp.CustomerPortal.Models.ViewModel.Identity.ApplicationDbContext,System.Int32]':无法解析类型的服务尝试激活“Microsoft.AspNetCore.Identity.EntityFrameworkCore.UserStore”时出现“TrussCorp.CustomerPortal.Models.ViewModel.Identity.ApplicationDbContext”4[TrussCorp.CustomerPortal.Models.ViewModel.Identity.UserViewModel,TrussCorp.CustomerPortal.Models.ViewModel.Identity.RoleViewModel,TrussCorp.CustomerPortal.Models.ViewModel.Identity.ApplicationDbContext,System.Int32]'.) (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Identity.IRoleStore1[TrussCorp.CustomerPortal.Models.ViewModel.Identity.RoleViewModel] 生命周期:作用域实现类型:Microsoft.AspNetCore.Identity.EntityFrameworkCore.RoleStore3[TrussCorp.CustomerPortal.Models.ViewModel.Identity.RoleViewModel,TrussCorp.CustomerPortal.Models.ViewModel.Identity.ApplicationDbContext,System.Int32]': Unable to resolve service for type 'TrussCorp.CustomerPortal.Models.ViewModel.Identity.ApplicationDbContext' while attempting to activate 'Microsoft.AspNetCore.Identity.EntityFrameworkCore.RoleStore3[TrussCorp.CustomerPortal.Models.ViewModel.Identity.RoleViewModel,TrussCorp.CustomerPortal.Models.ViewModel.Identity.ApplicationDbContext,System.Int32]'。) Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable serviceDescriptors、ServiceProviderOptions 选项)

Update

我回到了上一个问题的开始位置,但通过颠倒以下顺序解决了上述错误

    services.AddTransient<IUserStore<UserViewModel>, UserStore>();
        services.AddTransient<IRoleStore<RoleViewModel>, TrussCorp.CustomerPortal.Identity.RoleStore>();
        services.AddIdentity<UserViewModel,RoleViewModel>().AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders().AddRoles<RoleViewModel>();

但正如现在所说,我得到了Store 未实现 IUserRoleStore UserManager.GetUserRoleStore() https://stackoverflow.com/questions/58654400/store-does-not-implement-iuserrolestoretuser-usermanagertuser-getuserrolesto错误让我回到了第一个方向。


我对其他开发人员的建议是,如果您在互联网上找不到类似的问题,那么您正在做一些愚蠢而独特的事情。

解决方案是改变这个

public class UserStore : IUserStore<UserViewModel>, IUserPasswordStore<UserViewModel>, IUserRoleStore<UserRoleViewModel>
{

to this

public class UserStore : IUserStore<UserViewModel>, IUserPasswordStore<UserViewModel>, IUserRoleStore<UserViewModel>
{
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Identity Asp.net core 3 MVC 中创建服务 IUserStore 时出错 的相关文章

  • 使用 CLion 进行 OpenCV Windows 设置

    我想在 Windows 上为 CLion IDE 设置 OpenCV 我尝试使用 OpenCV 3 1 和 2 4 得到相同的结果 我有 Windows 10 64 位 CLion 使用 cygwin 环境 到目前为止我做了什么 1 从Op
  • CMake 找不到请求的 Boost 库

    既然我已经浏览了其他人的解决方案几个小时 但找不到适合我的问题的正确答案 我想将我的具体问题带给您 我正在尝试使用 CMake 构建 vsomeip 为此 我之前构建了 boost 1 55 但是 我在 CMake 中收到以下错误 The
  • 您可以从基本 Win32 控制台模板应用程序中的 C#/Winrt 组件调用(不是 WinForm/abstractions/wrappers 或使用 C++/Winrt 模板)吗?)

    我有一个现有的程序 win32 x86 控制台应用程序 需要调用托管代码 来自 Net 的 C dll The dll不暴露给 COM 但可以从 C WinRT 组件调用并由 C WinRT 控制台模板应用引用 BUT即使安装了 C Win
  • 在 OnModelCreating 期间设置列名称

    Issue 我目前正在尝试通过设置的属性为我的表及其列添加前缀 我正在使用实体框架核心 我已经正确地为表名添加了前缀 但我似乎无法弄清楚列的前缀 我有一种感觉 我需要使用反射 我已经留下了我的 可能很糟糕的 反思尝试 有人有办法在实体中设置
  • 从代码中,如何创建对存储在附加属性中的对象的属性的绑定?

    我们有一个继承的附加属性来存储一个对象 在可视化树的更下方 我们希望从代码绑定到该对象的属性 通常我们像这样构建绑定的路径部分 var someBinding new Binding Path new PropertyPath Attach
  • 运行需要 MySql.Data 的内置 .NET 应用程序

    我在运行我编写的内置 NET 应用程序时遇到问题 我的应用程序使用最新的 MySql 连接器 该连接器安装在我的系统上 当我尝试将其添加为引用时 该连接器显示为 NET 4 Framwork 组件 当我在环境中以调试模式运行应用程序时 一切
  • 是否存在指向不同类型的指针具有不同大小的平台?

    C 标准允许指向不同类型的指针具有不同的大小 例如sizeof char sizeof int 是允许的 但是 它确实要求如果将指针转换为void 然后转换回其原始类型 它必须与其原始值进行比较 因此 从逻辑上来说 sizeof void
  • 如何制作可启动程序?

    所以 这个问题可能看起来很奇怪 但假设我编译了 int main void int x 3 int y 4 int z x y 是否可以让CPU这样运行 如何 例如 这允许我写入监视器吗 如果我没记错的话 内存中有些地方可以写入要显示的内容
  • 计算另一个表达式中的 C# 表达式

    我想在另一个表达式中使用一个表达式 Expression
  • 为什么'enable_if'不能用于禁用这里声明

    include
  • 使用查询表达式对 List 进行排序

    我在使用 Linq 订购这样的结构时遇到问题 public class Person public int ID get set public List
  • 增强精神、递归和堆栈溢出

    为什么下面的代码在运行时崩溃 它会给出堆栈溢出错误 include
  • 如何在三个 IEnumerable 上使用 Zip [重复]

    这个问题在这里已经有答案了 可能的重复 使用 Linq 从 3 个集合创建项目 https stackoverflow com questions 5284315 create items from 3 collections using
  • C#6 中的长字符串插值行

    我发现 虽然字符串插值在应用于现有代码库的字符串 Format 调用时非常好 但考虑到通常首选的列限制 字符串对于单行来说很快就会变得太长 特别是当被插值的表达式很复杂时 使用格式字符串 您将获得一个可以拆分为多行的变量列表 var str
  • 如何停止无限循环?

    我正在编写一个程序 该程序将计算三角形或正方形的面积 然后提示用户是否希望计算另一个 我的代码已经运行到可以计算任一形状的面积的程度 但随后不再继续执行代码的其余部分 例如 如果选择了正方形 则计算面积 然后返回到正方形边长的提示 我假设这
  • CUDA 8 编译错误 -std=gnu++11

    我正在尝试转换一些代码以使用 CUDA 并且我认为我遇到了兼容性问题 我们使用CMake 这些是我使用的 gcc 和 CUDA 版本 gcc version gcc Ubuntu 5 4 0 6ubuntu1 16 04 5 5 4 0 2
  • 在 C#.NET 中安全删除文件

    在我正在做的一个项目中 我想为用户提供 安全 删除文件的选项 例如 用随机位或 0 覆盖它 在 C NET 中是否有一种简单的方法可以做到这一点 效果如何 你可以调用系统内部删除 http technet microsoft com en
  • LINQ 中的“from..where”或“FirstOrDefault”

    传统上 当我尝试从数据库中获取用户的数据时 我使用了以下方法 在某种程度上 DbUsers curUser context DbUsers FirstOrDefault x gt x u LoginName id string name c
  • INotifyPropertyChanged 和 propertyName

    我一直不确定它的含义propertyName实施时INotifyPropertyChanged 所以一般来说你实现INotifyPropertyChanged as public class Data INotifyPropertyChan
  • DataContractSerializer 事件/委托字段问题

    在我的 WPF 应用程序中 我正在使用DataContractSerializer序列化对象 我发现它无法序列化具有事件或委托声明的类型 考虑以下失败的代码 Serializable public abstract class BaseCl

随机推荐

  • 使用 FPDF 使文本在单元格中换行?

    现在 当我使用带有文本的单元格时 它全部保留在一行上 我知道我可以使用写入功能 但我希望能够指定高度和宽度 这就是我现在所拥有的 但正如我所说 文本不会换行以保留在尺寸中 pdf gt Cell 200 40 reportSubtitle
  • 为什么我们在 Hibernate 中使用 @Embeddable

    有什么用 Embedded and Embeddable处于休眠状态 因为我在互联网上找到的每个示例都是在单个表中插入数据并使用两个不同的类来执行此操作 我的观点是 如果我使用单个表 那么我可以映射单个类内的所有列 那么为什么我应该使用不同
  • 关闭mysql连接重要吗?

    关闭mysql连接效率是否至关重要 或者在php文件运行后它会自动关闭吗 来自文档 http us php net function mysql connect 注意 脚本执行结束后 到服务器的链接将立即关闭 除非通过显式调用 mysql
  • Xcode 4.4 约束错误

    我正在使用 Xcode 4 4 并在界面生成器中创建视图 当我运行我的应用程序时 我始终收到以下约束错误 Unable to simultaneously satisfy constraints
  • Realm React-Native:从 JS(反应本机代码)和 android(java)访问相同的领域

    我有一个用例 我在 android 代码 本机 中接收一些数据 我想将此数据插入到由我的反应代码打开或创建的同一领域中 或者在同一个领域写作 我怎样才能实现这个目标 感谢所有的帮助 附 我知道我可以以某种方式制作一个本机模块并将数据发送回我
  • 如何使用 JMH 按顺序运行基准测试中的方法?

    在我的场景中 基准测试中的方法应该在一个线程中顺序运行并按顺序修改状态 例如 有一个List
  • 在 API 网关路径中引用授权者定义

    我在我的 cloudformation 模板中定义了一个自定义授权者 MyCustomAuthorizer Type AWS ApiGateway Authorizer Properties Name MyCustomAuthorizer
  • MongoDB 中的多语言属性

    我正在尝试在 MongoDB 中设计一个模式范例 它将支持文档中变量属性的多语言值 例如 我有一个产品目录 其中每个产品可能需要以各种语言存储其名称 标题或任何其他属性 同样的范例可能也适用于其他特定于区域设置的属性 例如价格 货币变化 我
  • jquery数组分组依据

    我有这样的数组 abcArr A 10 B 20 A 30 C 40 如何按 A B C 对值进行分组和求和 each abcArr function if this 0 this 0 this 1 this 1 I know this w
  • 如何考虑标签将多行合并为一行

    我有一个数据框 每一行都包含一个句子 我想将这些行合并在一起 以便每行包含每个作者的 4 个句子 我有类似这样的数据框 text author sent1 x sent2 x sent3 x sent1002 x sent1 y sent2
  • 如何从一个表中选择另一表中不存在的所有记录?

    表1 ID 名称 表2 ID 名称 Query SELECT name FROM table2 that are not in table1 already SELECT t1 name FROM table1 t1 LEFT JOIN t
  • 两个linux内核模块之间是否可以通过netlink进行通信?

    众所周知 netlink是用户 内核空间的通信机制 我想从我的内核模块与另一个内核模块进行通信 另一个内核模块已经具有 netlink 接口 是否可以像我们在用户空间中那样从内核模块到 netlink 建立连接 简短回答 不 如果要在两个内
  • 证书颁发:来自 P7B 和 CRT 的 KEY 或 PFX

    我对证书很陌生 这是我第一次购买它 我生成了 CSR 文件 在 IIS 中 并使用 GoDaddy 网站购买了证书 他们给我发了两个文件 P7B 和 CRT 由于我将使用 Azure Web 角色的证书 因此我需要 PFX 如何仅使用 CS
  • 将 svn 存储库数据库格式从 1.6 降级到 1.5

    我确实遇到了一个不寻常的情况 因为我必须将 svn 存储库从 svn server 1 6 迁移到 svn server 1 5 问题是没有可用的降级工具 有人知道一些脚本或者有降级 svn 存储库的经验吗 Cheers Kevin 除了
  • 有没有办法预先缓存网页以便使用 Android WebView 查看?

    我读过有关 HTML5 缓存清单的内容 并且我看到 Android 确实支持使用缓存清单来缓存网站 我想使用缓存清单下载我的网站所需的所有资源以进行预加载 然后打开 WebView 并使用预缓存的资源显示远程网站 我想以某种方式预先缓存我的
  • 同时将 r 与 foreach 和 mclapply 并行

    我正在实现一个并行处理系统 该系统最终将部署在集群上 但我无法弄清楚并行处理的各种方法如何交互 我需要使用 for 循环来运行一大段代码 其中包含几个大的矩阵运算列表 为了加快速度 我想使用 foreach 并行化 for 循环 并使用 m
  • 对 C++ 类进行 STL 化

    我正在尝试编写一个包含多个 std vectors 作为数据成员的类 并提供向量接口的子集来访问它们 class Mesh public private std vector
  • 在应用程序引擎任务队列中传递多个参数(JAVA)

    有没有办法在 google app engine 的队列中传递多个参数 我使用下面的代码 Queue queue QueueFactory getQueue sms queue queue add TaskOptions Builder u
  • 为什么 Readonly 可以分配给 T,但 ReadonlyArray 不能分配给 Array

    type Foo a number let obj Foo as Readonly
  • 在 Identity Asp.net core 3 MVC 中创建服务 IUserStore 时出错

    Update 请参阅下面的更新 我问过一个之前的 stackoverflow 问题 https stackoverflow com questions 58654400 store does not implement iuserroles