简体   繁体   English

.net-core 认证问题

[英].net-core authentication issue

I'm going to authenticate the user by the help of .net-core version 2. after registration, "SqlNullValueException: Data is Null. This method or property cannot be called on Null values."我将在 .net-core 版本 2 的帮助下对用户进行身份验证。注册后,“SqlNullValueException:数据为 Null。无法在 ZBBB93EF26E3C101FF11CDD21CAB08A9 值上调用此方法或属性。” err will be appeared on signInManager method. err 将出现在 signInManager 方法上。

I've checked nullable datas and required datas in my model and database.我检查了 model 和数据库中的可空数据和所需数据。

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task < IActionResult > Login(LoginViewModel model, string returnUrl = null) {
  ViewData["ReturnUrl"] = returnUrl;
  if (ModelState.IsValid) {
   var result = await signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, false);

   var user = new ApplicationUser {
    Email = model.Email
   };

   // ...

startup.cs file启动.cs 文件

IConfigurationRoot configurationRoot;
public Startup(IHostingEnvironment env) {
 configurationRoot = new ConfigurationBuilder().SetBasePath(env.ContentRootPath)
  .AddJsonFile("appsettings.json").Build();
}

public void ConfigureServices(IServiceCollection services) {
 services.AddIdentity < ApplicationUser, IdentityRole > ()
  .AddEntityFrameworkStores < ApplicationDbContext > ()
  .AddDefaultTokenProviders();

 services.AddTransient < ApplicationDbContext > ();
 services.AddTransient < IEmailSender, AuthMessageServices > ();
 services.AddAuthentication();
 services.AddMvc();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
 if (env.IsDevelopment()) {
  app.UseDeveloperExceptionPage();
  app.UseBrowserLink();
 } else {
  app.UseExceptionHandler("/Error");
 }

 app.UseAuthentication();
 app.UseStaticFiles();

 app.UseMvc(routes => {
  routes.MapRoute(
   name: "default",
   template: "{controller=Home}/{action=Index}/{id?}");
 });
}

It looks like you forgot to initialize ApplicationDbContext看起来您忘记初始化 ApplicationDbContext

services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM