我在这里遵循了Deblokt教程,该教程详细介绍了如何在.net core 3.1中设置IdentityServer4并将AspNetCore Identity用于用户存储。
我可以从 https://localhost:5001/Account/Login 网址(和注销(登录到我的身份服务器就好了......但是我无法访问AspNet Core Identity页面(例如Register/ForgotPassword/AccessDened等(。我已经把它们搭建好了,所以它们位于区域/身份/页面/帐户下......但是当我导航到它们时,例如 https://localhost:5001/Identity/Account/Register,我只是找不到 404。
我不确定,但怀疑这可能是一个路由问题,所以我研究了为这些页面提供路由,但我看到的 IdentityServer 示例适用于 Core2.1 并使用该应用程序。使用MVC ...我的创业公司没有。 我试图包含它,但我收到错误说
端点路由不支持"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();
});
}
}
}
嗯,这是我的启动文件中的 1 行代码.cs
app.UseEndpoints(endpoints =>
{
endpoints.MapDefaultControllerRoute();
endpoints.MapRazorPages(); // This one!
});
哦,如果有人遇到它,注册页面上会发生另一个错误。
无法解析类型"Microsoft.AspNetCore.Identity.UI.Services.IEmailSender"的服务
只需将以下代码行添加到启动中的配置服务方法的末尾.cs
services.AddTransient<IEmailSender,SomeEmailSender>();
IEmailSender来自Microsoft.AspNetCore.Identity.UI.Services。只需创建一个实现此接口的类并在此处声明它。