【发布时间】:2020-01-24 02:33:46
【问题描述】:
所以我发生了一件有趣的事情。我正在创建一个新的 .NET Core MVC 应用程序,并且正在添加默认身份验证。现在生成的 _LoginPartial 有以下行:
<a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Login">Login</a>
但是当它在页面上渲染时,href 属性是这样出来的:
<a class="nav-link text-dark" href="/?area=Identity&page=%2FAccount%2FLogin">Login</a>
我真的不确定为什么会这样。当我尝试添加一个区域时,@Url.Action 也会发生这种情况。我一定是错过了什么,但我完全迷失了。
编辑:
这里是 ConfigureServices 和 Configure 方法。
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentity<ApplicationUser, MongoIdentityRole>()
.AddMongoDbStores<ApplicationUser, MongoIdentityRole, Guid>("mongodb://localhost:27017", "gw")
.AddSignInManager()
.AddDefaultTokenProviders();
services.AddMvc()
.AddRazorPagesOptions(options => {})
.SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_3_0);
services.Configure<IdentityOptions>(options => {
// Password settings.
options.Password.RequireDigit = true;
options.Password.RequireLowercase = true;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = true;
options.Password.RequiredLength = 8;
options.Password.RequiredUniqueChars = 1;
// Lockout settings.
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
options.Lockout.MaxFailedAccessAttempts = 5;
options.Lockout.AllowedForNewUsers = true;
// User settings.
options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._@+";
options.User.RequireUniqueEmail = false;
});
services.ConfigureApplicationCookie(options => {
// Cookie settings
options.Cookie.HttpOnly = true;
options.ExpireTimeSpan = TimeSpan.FromMinutes(5);
options.LoginPath = "/Identity/Account/Login";
options.AccessDeniedPath = "/Identity/Account/AccessDenied";
options.SlidingExpiration = true;
});
// services.AddTransient<IEmailSender, AuthMessageSender>();
// services.AddTransient<ISmsSender, AuthMessageSender>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
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.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints => {
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages();
});
}
【问题讨论】:
标签: c# authentication asp.net-core