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

达伦·伍德

更新资料

请参阅下面的更新。

我最近问了一个先前的stackoverflow问题,但是在实现以下两个方面的建议解决方案后,在ASP.NET Core中的身份模型自定义ASP.NET Core Identity的自定义存储提供程序中,我遇到了一个问题我只是无法获得解决方案。我已IdentityUser按照建议实施的扩展和的扩展IdentityRole我已经实现IUserPasswordStore,并IUserRoleStoreIUserStoreUserStore,我已经实现IRoleStoreRoleStore我还实现了新的dbContext ApplicationDbContext,它实现了IdentityDbContext由于构造函数不接受参数,因此出现了问题,因此我实现了一个新的构造函数。我不确定这是否正确。

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

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

错误是巨大的。我把它放在最后。在线搜索错误的各个部分并没有提供有关我在做什么的任何想法,并且由于我是ASP.NET Core 3和Identity的新手,所以我很困惑。

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

ConfigureServices方法

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";
        });
    }

ApplicationDbContext-这些都在同一个命名空间中

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();
        });
    }
}

UserViewModel

public class UserViewModel : IdentityUser<int>
{

UserRoleViewModel

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.IUserStore 1[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”时,无法解析类型为“ TrussCorp.CustomerPortal.Models.ViewModel.Identity.ApplicationDbContext”的服务.EntityFrameworkCore.UserStore 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选项)

更新资料

我回到了上一个问题的开头,但是通过反转以下命令的顺序解决了上述错误

    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()错误,这使我回到了平方。

达伦·伍德

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

解决的办法是改变这一点

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

对此

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在ASP.NET Identity中添加角色

来自分类Dev

ASP.Net Identity不创建用户

来自分类Dev

Thinktecture的Identity Server v3的ASP.NET MVC ViewService

来自分类Dev

Asp.Net MVC 6 Identity 3 MongoDB外部登录(Facebook)

来自分类Dev

ASP.NET Identity用户创建

来自分类Dev

如何在ASP.NET 5 Identity中设置PasswordHasherCompatibilityMode.IdentityV3?

来自分类Dev

Asp.Net核心MVC6如何在Identity 3中初始添加角色

来自分类Dev

使用ASP.NET Core 1.0 Identity UserManager进行的事务

来自分类Dev

ASP .NET Identity-自定义IUserStore FindByNameAsync方法

来自分类Dev

ASP.NET Core Identity 3 Cookie超时

来自分类Dev

如何在ASP.NET Core 3.0中解密.AspNetCore.Identity.Application cookie?

来自分类Dev

ASP.NET Core 3:无法从根提供程序解析作用域服务'Microsoft.AspNetCore.Identity.UserManager`1 [Alpha.Models.Identity.User]'

来自分类Dev

ASP.NET Core 3 MVC:对象列表的模型绑定

来自分类Dev

如何在ASP.NET Core Identity中扩展和验证会话?

来自分类Dev

在URI中包含子目录-ASP.NET Core 3.1 Identity Server

来自分类Dev

使用运行Identity Server 4的ASP.NET CORE 3身份验证服务器对ASP.NET MVC 5应用(目标.net 4.5)进行身份验证

来自分类Dev

删除ASP.NET Core Identity中已登录用户的策略

来自分类Dev

在asp.net core 3中创建漂亮的url

来自分类Dev

Identity如何在具有ASP.NET Core MVC 3.1的单个用户帐户的项目模板中工作?

来自分类Dev

如何在ASP.NET Core 2.2中使用MVC代替剃须刀页面来使用Identity

来自分类Dev

覆盖ASP.NET Core 3中的授权策略

来自分类Dev

如何在ASP.NET CORE MVC中覆盖默认的Identity AccessDenied路由

来自分类Dev

从ASP.NET MVC 3迁移到ASP.NET Identity 2.0

来自分类Dev

Asp.Net MVC 6 Identity 3 MongoDB外部登录(Facebook)

来自分类Dev

在MVC 5中的ASP.NET Identity 3上管理自定义用户属性

来自分类Dev

如何使用 ASP.NET Core、Identity Framework 和 Entity Framework 显示特定用户创建的内容

来自分类Dev

加载表时显示用户名(存储在 ASP.Net Core Identity 系统中),而不是 ID

来自分类Dev

登录链接不起作用,在 ASP.NET Core Identity UI 中

来自分类Dev

如何将新列添加到 Identity RoleClaims 表中(asp net core)

Related 相关文章

  1. 1

    在ASP.NET Identity中添加角色

  2. 2

    ASP.Net Identity不创建用户

  3. 3

    Thinktecture的Identity Server v3的ASP.NET MVC ViewService

  4. 4

    Asp.Net MVC 6 Identity 3 MongoDB外部登录(Facebook)

  5. 5

    ASP.NET Identity用户创建

  6. 6

    如何在ASP.NET 5 Identity中设置PasswordHasherCompatibilityMode.IdentityV3?

  7. 7

    Asp.Net核心MVC6如何在Identity 3中初始添加角色

  8. 8

    使用ASP.NET Core 1.0 Identity UserManager进行的事务

  9. 9

    ASP .NET Identity-自定义IUserStore FindByNameAsync方法

  10. 10

    ASP.NET Core Identity 3 Cookie超时

  11. 11

    如何在ASP.NET Core 3.0中解密.AspNetCore.Identity.Application cookie?

  12. 12

    ASP.NET Core 3:无法从根提供程序解析作用域服务'Microsoft.AspNetCore.Identity.UserManager`1 [Alpha.Models.Identity.User]'

  13. 13

    ASP.NET Core 3 MVC:对象列表的模型绑定

  14. 14

    如何在ASP.NET Core Identity中扩展和验证会话?

  15. 15

    在URI中包含子目录-ASP.NET Core 3.1 Identity Server

  16. 16

    使用运行Identity Server 4的ASP.NET CORE 3身份验证服务器对ASP.NET MVC 5应用(目标.net 4.5)进行身份验证

  17. 17

    删除ASP.NET Core Identity中已登录用户的策略

  18. 18

    在asp.net core 3中创建漂亮的url

  19. 19

    Identity如何在具有ASP.NET Core MVC 3.1的单个用户帐户的项目模板中工作?

  20. 20

    如何在ASP.NET Core 2.2中使用MVC代替剃须刀页面来使用Identity

  21. 21

    覆盖ASP.NET Core 3中的授权策略

  22. 22

    如何在ASP.NET CORE MVC中覆盖默认的Identity AccessDenied路由

  23. 23

    从ASP.NET MVC 3迁移到ASP.NET Identity 2.0

  24. 24

    Asp.Net MVC 6 Identity 3 MongoDB外部登录(Facebook)

  25. 25

    在MVC 5中的ASP.NET Identity 3上管理自定义用户属性

  26. 26

    如何使用 ASP.NET Core、Identity Framework 和 Entity Framework 显示特定用户创建的内容

  27. 27

    加载表时显示用户名(存储在 ASP.Net Core Identity 系统中),而不是 ID

  28. 28

    登录链接不起作用,在 ASP.NET Core Identity UI 中

  29. 29

    如何将新列添加到 Identity RoleClaims 表中(asp net core)

热门标签

归档