asp.net core 1.1身份项目升级到3.0

时间:2019-10-31 16:51:44

标签: c# asp.net-core

最近将asp.net core 1.1项目升级到3.0项目。所有软件包均已更新。和startup.cs修复程序都是根据我在网上找到的文档完成的。

在以前使用的现有代码中,用户,角色和声明区域出现一系列错误。我需要找到一种数据库更改最少的解决方案。我很确定我错过了一些简单的东西。

这里是示例代码,其中角色没有声明。 出现错误CS1061
“角色”不包含“权利要求”的定义,并且找不到可访问的扩展方法“权利要求”接受类型为“角色”的第一个参数(您是否缺少using指令或程序集引用?)

var roles = await _db.Roles.Where(x => x.Claims.Any(y => y.ClaimType == "Leadership")).ToListAsync().ConfigureAwait(false);

CS1061'用户'不包含'角色'的定义,并且找不到可以接受的扩展方法'角色'接受类型为'用户'的第一个参数(您是否缺少using指令或程序集引用?)< / p>

await _db.Users.Include(x => x.Roles).....

我对用户和角色具有自定义定义,并添加了变量。

public class User : IdentityUser<int>
public class Role : IdentityRole<int>

这是模型的创建

public class ApplicationDbContext : IdentityDbContext<User, Role, int>
{
 ........
    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.HasDefaultSchema("core");
        builder.Entity<User>().ToTable("Users", "core");
        builder.Entity<Role>().ToTable("Roles", "core");
        builder.Entity<IdentityUserClaim<int>>().ToTable("UserClaims", "core");
        builder.Entity<IdentityUserRole<int>>().ToTable("UserRoles", "core").HasKey(x => new { x.UserId, x.RoleId });
        builder.Entity<IdentityUserLogin<int>>().ToTable("UserExternalLogins", "core").HasKey(x => x.ProviderKey);
        builder.Entity<IdentityUserToken<int>>().ToTable("UserTokens", "core").HasKey(x => new { x.UserId, x.Value });
        builder.Entity<IdentityRoleClaim<int>>().ToTable("RoleClaims", "core");

以及ConfigureServices的startup.cs部分

    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.            
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), 
            sqlServerOptionsAction: sqlOptions =>
            {
                sqlOptions.EnableRetryOnFailure(
                maxRetryCount: 10,
                maxRetryDelay: TimeSpan.FromSeconds(30),
                errorNumbersToAdd: null);
            }), ServiceLifetime.Transient);
        services.AddIdentity<User, Role>(options => options.SignIn.RequireConfirmedAccount = true)
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();
        services.AddControllersWithViews();
        services.AddRazorPages();
        services.Configure<IdentityOptions>(options =>
        {
            // Default Lockout settings.
            options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30);
            options.Lockout.MaxFailedAccessAttempts = 10;
            options.Lockout.AllowedForNewUsers = true;
            options.Password.RequireDigit = true;
            options.Password.RequireLowercase = true;
            options.Password.RequireNonAlphanumeric = false;
            options.Password.RequireUppercase = false;
            options.Password.RequiredLength = 6;
            options.User.RequireUniqueEmail = true;
        });
        services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
        .AddCookie(options =>
        {
            options.LoginPath = "/login";
            options.LogoutPath = "/logout";
            options.AccessDeniedPath = "/unauthorized";
            options.Cookie.Expiration = TimeSpan.FromDays(150);
        });
        services.AddSingleton<IAuthorizationHandler, AuthorizeMemberHandler>();
        services.AddSingleton<IAuthorizationHandler, AuthorizeMemberOrBotHandler>();
        services.AddAuthorization(options =>
        {
            options.AddPolicy("Mgmt", policy => policy.RequireClaim("Mgmt"));
            options.AddPolicy("Member", policy => policy.Requirements.Add(new MemberRequirement()));
            options.AddPolicy("MemberOrBot", policy => policy.Requirements.Add(new MemberOrBotRequirement()));
        });

        services.AddScoped<ShoppingBag>();
        services.Configure<FormOptions>(x => x.ValueCountLimit = 2048);

        services.AddMvc();
        services.AddRouting(options => {
            options.LowercaseUrls = true;
            options.AppendTrailingSlash = true;
        });
        services.AddMvc(options => options.EnableEndpointRouting = false);

如果我忘记了任何信息,请告诉我,谢谢。

0 个答案:

没有答案