我的AspNet Core 3.1身份页面在IdentityServer4中给出404。如何访问这些?这是路线问题吗?

问题描述 投票:0回答:1

我遵循了Deblokt教程here,该教程详细解释了如何在.net core 3.1中设置IdentityServer4以及如何将AspNetCore Identity用于用户存储。

我可以从https://localhost:5001/Account/Login URL登录到我的IdentityServer(并注销)。。。但是我无法访问AspNet核心身份页面(例如Register / ForgotPassword / AccessDenied等)。我已经将它们脚手架出了,所以它们位于Areas / Identity / Pages / Account ...下,但是当我导航到它们时,例如https://localhost:5001/Identity/Account/Register,我只会找到404。

我不确定,但是怀疑这可能是路由问题,所以我研究了为这些页面提供路由,但是我为IdentityServer看到的示例是针对Core2.1并使用app.UseMVC ...我的创业公司没有。我尝试将其包含在内,但是出现错误,提示我>>

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

那我从这里去哪里?

我从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();
            });
        }
    }
}
    

我在这里遵循了Deblokt教程,该教程详细解释了如何在.net core 3.1中设置IdentityServer4以及如何将AspNetCore Identity用于用户存储。我可以登录到我的...

routes asp.net-identity identityserver4 razor-pages asp.net-core-3.1
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.