我的 AspNet Core 3.1 身份页面在 IdentityServer4 中给出 404。我怎样才能访问这些?这是路线问题吗?
Posted
技术标签:
【中文标题】我的 AspNet Core 3.1 身份页面在 IdentityServer4 中给出 404。我怎样才能访问这些?这是路线问题吗?【英文标题】:My AspNet Core 3.1 Identity Pages give a 404 in IdentityServer4. How can I access these? Is this a route problem? 【发布时间】:2020-10-07 18:30:59 【问题描述】:我遵循了 Deblokt 教程 here,它详细解释了如何在 .net core 3.1 中设置 IdentityServer4 并将 AspNetCore Identity 用于用户存储。
我可以从https://localhost:5001/Account/Login url 登录到我的IdentityServer(和注销)就好了......但我无法访问AspNet Core Identity 页面(例如Register/ForgotPassword/AccessDenied 等)。我已经把它们搭建好了,所以它们位于区域/身份/页面/帐户下...但是当我导航到它们时,例如https://localhost:5001/Identity/Account/Register,我只是得到一个 404 未找到。
我不确定,但怀疑这可能是路由问题,所以我研究了为这些页面提供路由,但我看到的 IdentityServer 示例是针对 Core2.1 并使用 app.UseMVC ...我的创业公司没有。我试图包含它,但我收到错误提示
端点路由不支持“IApplicationBuilder.UseMvc(...)”。 使用“IApplicationBuilder.UseMvc”集 '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();
);
【问题讨论】:
【参考方案1】:嗯,这是我的 startup.cs 文件中的 1 行代码
app.UseEndpoints(endpoints =>
endpoints.MapDefaultControllerRoute();
endpoints.MapRazorPages(); // This one!
);
哦,如果有人遇到它,注册页面上会出现另一个错误。
无法解析类型“Microsoft.AspNetCore.Identity.UI.Services.IEmailSender”的服务
只需将以下代码行添加到 Startup.cs 中 ConfigureServices 方法的末尾
services.AddTransient<IEmailSender,SomeEmailSender>();
IEmailSender 来自 Microsoft.AspNetCore.Identity.UI.Services。只需创建一个实现该接口的类并在此处声明即可。
【讨论】:
以上是关于我的 AspNet Core 3.1 身份页面在 IdentityServer4 中给出 404。我怎样才能访问这些?这是路线问题吗?的主要内容,如果未能解决你的问题,请参考以下文章
Azure AD 在 .NET Core 3.1 中未进行身份验证
用于 aspnet-core web api 的基于令牌的身份验证
使用 AspNet Core 2.0 进行 Google JWT 身份验证
AspNet Core 脚手架应用程序中的登录和注册页面在哪里?