你好。ASP.NET Core项目没有验证功能,因此我尝试添加内置的身份验证功能。数据库中的表已经成功创建,新用户已注册。在帐户控制器的登录方法中,SignInManager
返回成功的处理结果。在局部视图中,链接用于添加注入和库进行登录\注册(所有内容都是基于身份验证的默认项目实现):
@using Microsoft.AspNetCore.Identity
@inject SignInManager<ApplicationUser> SignInManager
然后在视图检查中:
SignInManager.IsSignedIn(User)
登录后,它总是为false,不管之前的状态是否成功。我在控制器中添加了什么东西,或者在Startup.cs
的Configuration
中漏掉了什么?所有事情都按照默认的VS项目进行了实现,包括身份验证。
更新:Startup.cs
中的配置:
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseMySql("Server=; Database=; Uid=;Pwd=;"));
services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.Password.RequireDigit = true;
options.Password.RequiredLength = 6;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireUppercase = true;
options.Password.RequireLowercase = false;
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
options.Lockout.MaxFailedAccessAttempts = 5;
options.User.RequireUniqueEmail = true;
options.SignIn.RequireConfirmedEmail = true;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// Here added my application services.
.......
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}