【问题标题】:Blazor WASM Hosted with Authentication Breaks when upgrading to .net 6升级到 .net 6 时托管的 Blazor WASM 身份验证中断
【发布时间】:2021-12-27 18:40:01
【问题描述】:

我有一个使用用户身份验证托管的 Blazor WASM,它正在运行 .net5,我已升级到 .net 6。但是,我现在在尝试单击登录按钮时收到 500 错误。

我看了下面的

https://docs.duendesoftware.com/identityserver/v5/upgrades/is4_v4_to_dis_v5/

我将所有项目升级到 .net 6 并将所有 NugetPackages 升级到最新版本。 我更新了仅在 DataContext 上使用的命名空间

public class DataContext : ApiAuthorizationDbContext<ApplicationUser>
    {
        public DataContext(DbContextOptions options, IOptions<OperationalStoreOptions> operationalStoreOptions) : base(options, operationalStoreOptions)
        {
            ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
        }

这是我的服务器 Startup.cs

public void ConfigureServices(IServiceCollection services)
        {
            //Register the Datacontext and Connection String
            services.AddDbContext<DataContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));

            services.AddDatabaseDeveloperPageExceptionFilter();

            //Sets up the default Asp.net core Identity Screens - Use Identity Scaffolding to override defaults
            services.AddDefaultIdentity<ApplicationUser>( options =>
                    {
                        options.SignIn.RequireConfirmedAccount = true;
                        options.Password.RequireDigit = true;
                        options.Password.RequireLowercase = true;
                        options.Password.RequireUppercase = true;
                        options.Password.RequiredUniqueChars = 0;
                        options.Password.RequireNonAlphanumeric = false;
                        options.Password.RequiredLength = 8;
                        options.User.RequireUniqueEmail = true;
                    })
                .AddRoles<IdentityRole>()
                .AddEntityFrameworkStores<DataContext>();

            //Associates the User to Context with Identity
            services.AddIdentityServer()
                .AddApiAuthorization<ApplicationUser, DataContext>( options =>
            {
                options.IdentityResources["openid"].UserClaims.Add(JwtClaimTypes.Role);
                options.ApiResources.Single().UserClaims.Add(JwtClaimTypes.Role);
            });
            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Remove(JwtClaimTypes.Role);

            //Adds authentication handler
            services.AddAuthentication().AddIdentityServerJwt();

            services.AddHttpContextAccessor();
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, DataContext dataContext)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseMigrationsEndPoint();
                app.UseWebAssemblyDebugging();
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            //AutoMigrates data
            dataContext.Database.Migrate();

            app.UseHttpsRedirection();
            app.UseBlazorFrameworkFiles();
            app.UseStaticFiles();

            app.UseSerilogIngestion();
            app.UseSerilogRequestLogging();

            app.UseRouting();

            app.UseIdentityServer();
            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
                endpoints.MapControllers();
                endpoints.MapFallbackToFile("index.html");
            });
        }

我将 DbSet 键添加到 DataContext 并在包管理器控制台中使用以下内容运行迁移

add-migration UpdateToDuende
update-database

但是,每次我点击登录按钮时,我都会得到一个 500 和以下内容

【问题讨论】:

    标签: blazor identityserver4 duende-identity-server


    【解决方案1】:

    在升级过程中,表 dbo.keys 似乎已从数据库中删除。升级后第一次运行应用程序时,它也会被删除(至少对我而言)。一条消息确实出现在某个地方,但很难找到。数据库的备份应该有 dbo.keys 表,只需从那里编写脚本并运行它。运行应用程序。再次运行脚本,问题就消失了,至少对我而言。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-11
      • 2021-11-05
      • 2021-03-01
      • 2021-07-24
      • 2021-03-02
      • 2021-04-30
      • 1970-01-01
      • 2021-04-11
      相关资源
      最近更新 更多