【问题标题】:Asp.Net Core 3.1 Identity LinqToDB.Identity UserStore ProblemAsp.Net Core 3.1 Identity LinqToDB.Identity UserStore 问题
【发布时间】:2020-11-28 21:59:49
【问题描述】:

我一直在尝试为我的 .Net Core 3.1 项目实施 .Net Identity。在我的项目中,我使用 LinqToDB 来使用数据库。如,MySQL、MSSQL。我已经实现了我的身份类,如 AppUser、AppRole、AppUserClaim,并使用 LinqToDB 将它们创建到我的 mssql 数据库中。问题是,当项目运行时,它会返回一个这样的错误。

GenericArguments1, 'DevPlatform.Core.Domain.Identity.AppUser', on 'LinqToDB.Identity.UserStore`7[TKey,TUser,TRole,TUserClaim,TUserRole,TUserLogin,TUserToken]' 违反了类型的约束'TUser'。'

TypeLoadException: GenericArguments1, 'DevPlatform.Core.Domain.Identity.AppUser', on 'LinqToDB.Identity.UserStore`7[TKey,TUser,TRole,TUserClaim,TUserRole,TUserLogin,TUserToken]' 违反了约束类型参数“TUser”。

我在该代码块中收到此错误;

 public static void AddDevPlatformAuthentication(this IServiceCollection services, IConfiguration configuration)
    {
        services.AddIdentity<AppUser, LinqToDB.Identity.IdentityRole>(options =>
        {
            options.Password.RequireDigit = true;
            options.Password.RequiredLength = 4;
            options.Password.RequireNonAlphanumeric = false;
            options.Password.RequireUppercase = true;
            options.Password.RequireLowercase = false;

            options.User.RequireUniqueEmail = true;

            //TODO
            //options.User.RequireUniqueEmail = true; 
            //options.SignIn.RequireConfirmedEmail = true;
            //options.Lockout.MaxFailedAccessAttempts = 5;
            //options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(3);

        }).AddLinqToDBStores(new DefaultConnectionFactory())
            .AddDefaultTokenProviders();

        services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();

        // Uncomment the following lines to enable logging in with third party login providers

        JwtTokenDefinitions.LoadFromConfiguration(configuration);
        services.ConfigureJwtAuthentication();
        services.ConfigureJwtAuthorization();
    }

这是我继承自 IdentityUser 类的 AppUser 类。

using DevPlatform.Core.Entities;
using LinqToDB.Identity;
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace DevPlatform.Core.Domain.Identity
{

public class AppUser : IdentityUser<int>, IEntity
{
    public DateTime CreatedDate { get; set; }
    public DateTime? ModifiedDate { get; set; }

    public int? CreatedBy { get; set; }
    public int? ModifiedBy { get; set; }
    public int? StatusId { get; set; }

    [Required]
    [ForeignKey("UserDetail")]
    public int DetailId { get; set; }

    public virtual AppUserDetail UserDetail { get; set; }
}
}

代码块太多了,这里就不贴了。如果有人可以提供帮助,我将非常高兴。

如果你想看项目,可以在这里查看;

https://github.com/dogaanismail/DevPlatform

【问题讨论】:

    标签: asp.net-core asp.net-identity


    【解决方案1】:

    这个问题已经通过编辑 AddLinqToDBStores 解决了。

     public static void AddDevPlatformAuthentication(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddIdentity<AppUser, LinqToDB.Identity.IdentityRole<int>>(options =>
            {
                options.Password.RequireDigit = true;
                options.Password.RequiredLength = 4;
                options.Password.RequireNonAlphanumeric = false;
                options.Password.RequireUppercase = true;
                options.Password.RequireLowercase = false;
    
                options.User.RequireUniqueEmail = true;
    
                //TODO
                //options.User.RequireUniqueEmail = true; 
                //options.SignIn.RequireConfirmedEmail = true;
                //options.Lockout.MaxFailedAccessAttempts = 5;
                //options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(3);
    
            }).AddLinqToDBStores<int, AppUserClaim, AppUserRole, AppUserLogin, AppUserToken, AppRoleClaim>(new
            IdentityConnectionFactory(new SqlServerDataProvider(ProviderName.SqlServer, SqlServerVersion.v2017), "SqlServerIdentity", DataSettingsManager.LoadSettings().ConnectionString))
                .AddDefaultTokenProviders();
    
            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
    
            // Uncomment the following lines to enable logging in with third party login providers
    
            JwtTokenDefinitions.LoadFromConfiguration(configuration);
            services.ConfigureJwtAuthentication();
            services.ConfigureJwtAuthorization();
        }
    

    【讨论】:

      猜你喜欢
      • 2020-11-29
      • 2018-11-23
      • 2016-07-31
      • 2020-11-15
      • 2021-02-11
      • 1970-01-01
      • 2018-07-04
      • 2020-08-02
      • 1970-01-01
      相关资源
      最近更新 更多