首页 > 解决方案 > 从 2.2 升级到 3 后 ASP.Net Core 身份验证不起作用

问题描述

我在 ASP.NET Core 2.2 中有一个启用身份验证的 Web 应用程序(类型 MVC)(这是使用 dotnet aspnet-codegenerator 身份生成的)。一切都很好,直到那里。我不得不将项目升级到 ASP.Net Core 3.0 并且身份验证停止工作。有谁能够帮我 ?

我想我已经在Microsoft 的文档中提出了所描述的观点,但我不知道问题出在哪里。

项目参考:

<ItemGroup>        
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.0.0" />        
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0" />        
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.0.0" />        
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0" />        
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>        
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="3.0.0" />        
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.0.0" />        
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Utils" Version="3.0.0" />        
<PackageReference Include="RestSharp" Version="106.6.10" />
<PackageReference Include="Serilog" Version="2.9.0" />
<PackageReference Include="System.Linq.Queryable" Version="4.3.0" />

当我在 VS 中调试项目时,选项登录导致:localhost/?area=Identity&page=%2FAccount%2FLogin

在迁移之前,它导致:localhost/Identity/Account/Login

如果我手动加载它,它会给出错误 404

public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();

        services.Configure<CookiePolicyOptions>(options =>
        {
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddDbContext<WalletContext>(item => item.UseSqlServer(Configuration.GetConnectionString("WalletConn")));
        services.AddCloudscribePagination();
        services.AddAuthentication();
    }

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/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();
        }

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

        app.UseCookiePolicy();

        app.UseRouting();                  

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

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");

            endpoints.MapControllerRoute(
               name: "pagingMovements",
               pattern: "pager/{p:int?}",
               defaults: new { controller = "Movements", action = "Details" }
               );
        });
    }

类 IdentityHostingStartup [程序集:HostingStartup(typeof(GLB.Wallet.Backoffice.Areas.Identity.IdentityHostingStartup))]

public class IdentityHostingStartup : IHostingStartup
{
    public void Configure(IWebHostBuilder builder)
    {
        builder.ConfigureServices((context, services) => {
            services.AddDbContext<IdentityDataContext>(options =>
                options.UseSqlServer(
                    context.Configuration.GetConnectionString("BackofficeConn")));

            services.AddDefaultIdentity<IdentityUser>()
                .AddEntityFrameworkStores<IdentityDataContext>();
        });
    }
}

标签: c#asp.net-coreasp.net-core-3.0

解决方案


推荐阅读