简体   繁体   中英

.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." err will be appeared on signInManager method.

I've checked nullable datas and required datas in my model and database.

[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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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