我的 AspNet Core 3.1 身份页面在 IdentityServer4 中给出了 404。我怎样才能访问这些?这是路径问题吗?

Cra*_*aig 9 routes asp.net-identity identityserver4 razor-pages asp.net-core-3.1

我遵循了这里的 Deblokt 教程该教程详细解释了如何在 .net core 3.1 中设置 IdentityServer4 并为用户存储使用 AspNetCore Identity。

我可以从https://localhost:5001/Account/Login url(和注销)登录到我的 IdentityServer就好了......但我无法访问 AspNet Core Identity 页面(例如 Register/ForgotPassword/AccessDenied 等)。我已经将它们放在脚手架上,所以它们位于 Areas/Identity/Pages/Account 下...但是当我导航到它们时,例如https://localhost:5001/Identity/Account/Register,我只是找不到 404。

我不确定,但怀疑这可能是路由问题,所以我研究了为这些页面提供路由,但我看到的 IdentityServer 示例适用于 Core2.1 并使用 app.UseMVC ...我的启动不具有。我试图包含它,但我收到错误消息

端点路由不支持“IApplicationBuilder.UseMvc(...)”。要使用 'IApplicationBuilder.UseMvc' 在 'ConfigureServices(...)' 中设置 'MvcOptions.EnableEndpointRouting = false'

那么我该往哪里去呢?

我从 GitHub 下载了随附解决方案的教程,但它是 Core 2.1 解决方案,因此怀疑该教程是重写/升级到 3.1 的版本。

这是我的启动文件...

public class Startup
{

        public IWebHostEnvironment Environment { get; }
        public IConfiguration Configuration { get; }
        public Startup(IWebHostEnvironment environment, IConfiguration configuration)
        {
            Environment = environment;
            Configuration = configuration;
        }

        public void ConfigureServices(IServiceCollection services)
        {
            // uncomment, if you want to add an MVC-based UI
            services.AddControllersWithViews();
            services.AddRazorPages(); // I recently added this, but made no difference

            string connectionString = Configuration.GetConnectionString("DefaultConnection");

            var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name;

            services.AddDbContext<IdentityDbContext>(options =>
                options.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly))
            );

            services.AddDbContext<Data.ConfigurationDbContext>(options => options.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly)));

            services.AddIdentity<ApplicationUser, IdentityRole>(options =>
            {
                options.SignIn.RequireConfirmedEmail = true;
            })
            .AddEntityFrameworkStores<IdentityDbContext>()
            .AddDefaultTokenProviders();

            var builder = services.AddIdentityServer(options =>
            {
                options.Events.RaiseErrorEvents = true;
                options.Events.RaiseInformationEvents = true;
                options.Events.RaiseFailureEvents = true;
                options.Events.RaiseSuccessEvents = true;
                options.UserInteraction.LoginUrl = "/Account/Login";
                options.UserInteraction.LogoutUrl = "/Account/Logout";
                options.Authentication = new AuthenticationOptions()
                {
                    CookieLifetime = TimeSpan.FromHours(10), // ID server cookie timeout set to 10 hours
                    CookieSlidingExpiration = true
                };
            })
            .AddConfigurationStore(options =>
            {
                options.ConfigureDbContext = b => b.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly));
            })
            .AddOperationalStore(options =>
            {
                options.ConfigureDbContext = b => b.UseSqlServer(connectionString, sql => sql.MigrationsAssembly(migrationsAssembly));
                options.EnableTokenCleanup = true;
            })
            .AddAspNetIdentity<ApplicationUser>();

            X509Certificate2 cert = null;

            using (X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser))
            {
                certStore.Open(OpenFlags.ReadOnly);
                X509Certificate2Collection certCollection = certStore.Certificates.Find(
                    X509FindType.FindByThumbprint,
                    // Replace below with your cert's thumbprint
                    "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" // Replaced for this post
                    ,
                    false);
                // Get the first cert with the thumbprint
                if (certCollection.Count > 0)
                {
                    cert = certCollection[0];
                }
            }

            // Fallback to local file for development
            if (cert == null)
            {
                cert = new X509Certificate2(Path.Combine(Environment.ContentRootPath, "xxxxxxxxx.pfx"), "xxxxxxxxxxxxxxxxxxxxx"); // Replaced for this post
            }

            // not recommended for production - you need to store your key material somewhere secure
            //builder.AddDeveloperSigningCredential();
            builder.AddSigningCredential(cert);
            builder.AddValidationKey(cert);

            services.AddScoped<IProfileService, ProfileService>();

        }

        public void Configure(IApplicationBuilder app)
        {
            if (Environment.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            // uncomment if you want to add MVC
            app.UseStaticFiles();
            app.UseRouting();

            app.UseIdentityServer();

            // uncomment, if you want to add MVC
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapDefaultControllerRoute();
            });
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Cra*_*aig 20

嗯,这是我的 startup.cs 文件中的 1 行代码

app.UseEndpoints(endpoints =>
{
    endpoints.MapDefaultControllerRoute();
    endpoints.MapRazorPages(); // This one!
});
Run Code Online (Sandbox Code Playgroud)

哦,如果有人遇到它,注册页面上会发生另一个错误。

无法解析“Microsoft.AspNetCore.Identity.UI.Services.IEmailSender”类型的服务

只需将以下代码行添加到 Startup.cs 中的 ConfigureServices 方法的末尾

services.AddTransient<IEmailSender,SomeEmailSender>();

IEmailSender 来自 Microsoft.AspNetCore.Identity.UI.Services。只需创建一个实现此接口的类并在此处声明即可。