簡體   English   中英

.net-core 認證問題

[英].net-core authentication issue

我將在 .net-core 版本 2 的幫助下對用戶進行身份驗證。注冊后,“SqlNullValueException:數據為 Null。無法在 ZBBB93EF26E3C101FF11CDD21CAB08A9 值上調用此方法或屬性。” err 將出現在 signInManager 方法上。

我檢查了 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
   };

   // ...

啟動.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?}");
 });
}

看起來您忘記初始化 ApplicationDbContext

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM