我要做的是:
使用Identity Core设置登录/注销,主键为Int。NET核心文档:配置标识主键数据类型。(他们使用GUID,但想法是一样的)。设置非常成功,ID是整数,数据库正在填充。
然而,当我尝试使用内置的SignInManager<ApplicationUser>
登录并调用方法时
var result = await _signInManager.PasswordSignInAsync(Username,Password, RememberMe, lockoutOnFailure: false);
我得到以下错误:
系统。ArgumentNullException:值不能为null。参数名称:值
我确信SecurityTimeStamp已填满,并且服务已添加到Startup.cs 中
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext, int>()
.AddDefaultTokenProviders();
我的课程:
应用程序用户
public class ApplicationUser : IdentityUser<int>
{
}
应用程序角色
public class ApplicationRole : IdentityRole<int>
{
}
ApplicationDbContext
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
Stacktrace
系统。ArgumentNullException:值不能为null。参数名称:value在系统中。安全索赔。宣称ctor(字符串类型,字符串值,字符串值类型,字符串颁发者,字符串原始颁发者,ClaimsIdentity主题,字符串属性键,字符串属性值)在系统中。安全索赔。宣称ctor(字符串类型,字符串值)在微软。AspNetCore。身份用户声明主工厂
2.<CreateAsync>d__13.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Identity.SignInManager
1.d__24.MoveNext()---从引发异常的前一位置开始的堆栈结尾跟踪---在系统中。运行时。编译器服务。任务等待者。ThrowForNonSuccess(任务任务)在系统中。运行时。编译器服务。任务等待者。HandleNonSuccessAndDebuggerNotification(任务任务)在微软。AspNetCore。身份SignInManager1.<SignInAsync>d__29.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Identity.SignInManager
1.d__46.MoveNext()---从引发异常的前一位置开始的堆栈结尾跟踪---在系统中。运行时。编译器服务。任务等待者。ThrowForNonSuccess(任务任务)在系统中。运行时。编译器服务。任务等待者。HandleNonSuccessAndDebuggerNotification(任务任务)在微软。AspNetCore。身份SignInManager1.<PasswordSignInAsync>d__32.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Identity.SignInManager
1.d__33.MoveNext()---从引发异常的前一位置开始的堆栈结尾跟踪---在系统中。运行时。编译器服务。任务等待者。ThrowForNonSuccess(任务任务)在系统中。运行时。编译器服务。任务等待者。HandleNonSuccessAndDebuggerNotification(任务任务)在系统中。运行时。编译器服务。任务等待者1.GetResult() at Controllers.AccountController.<Login>d__7.MoveNext() in C:srcControllersAccountController.cs:line 64 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionFilterAsync>d__28.MoveNext() --- End of stack trace from previous location where exception was thrown --- at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeAsync>d__18.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware
1.d__18.MoveNext()---从引发异常的前一位置开始的堆栈结尾跟踪---在微软。AspNetCore。身份验证。身份验证中间件1.<Invoke>d__18.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware
1.d__18.MoveNext()---从引发异常的前一位置开始的堆栈结尾跟踪---在微软。AspNetCore。身份验证。身份验证中间件1.<Invoke>d__18.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware
1.d__18.MoveNext()---从引发异常的前一位置开始的堆栈结尾跟踪---在微软。AspNetCore。身份验证。身份验证中间件1.<Invoke>d__18.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware
1.d__18.MoveNext()---从引发异常的前一位置开始的堆栈结尾跟踪---在微软。AspNetCore。身份验证。身份验证中间件`1.d__18.MoveNext()---从引发异常的前一位置开始的堆栈结尾跟踪---在系统中。运行时。编译器服务。任务等待者。ThrowForNonSuccess(任务任务)在系统中。运行时。编译器服务。任务等待者。HandleNonSuccessAndDebuggerNotification(任务任务)在微软。ApplicationInsights。AspNetCore。异常跟踪中间件.d_4.MoveNext()---从引发异常的前一位置开始的堆栈结尾跟踪---在系统中。运行时。编译器服务。任务等待者。ThrowForNonSuccess(任务任务)在系统中。运行时。编译器服务。任务等待者。HandleNonSuccessAndDebuggerNotification(任务任务)在微软。AspNetCore。诊断。EntityFrameworkCore。迁移端点中间件.d_5.MoveNext()---从引发异常的前一位置开始的堆栈结尾跟踪---在系统中。运行时。编译器服务。任务等待者。ThrowForNonSuccess(任务任务)在系统中。运行时。编译器服务。任务等待者。HandleNonSuccessAndDebuggerNotification(任务任务)在微软。AspNetCore。诊断。EntityFrameworkCore。数据库错误页面中间件.d_6.MoveNext()---从引发异常的前一位置开始的堆栈结尾跟踪---在微软。AspNetCore。诊断。EntityFrameworkCore。数据库错误页面中间件.d_6.MoveNext()---从引发异常的前一位置开始的堆栈结尾跟踪---在系统中。运行时。编译器服务。任务等待者。ThrowForNonSuccess(任务任务)在系统中。运行时。编译器服务。任务等待者。HandleNonSuccessAndDebuggerNotification(任务任务)在微软。AspNetCore。诊断。DeveloperExceptionPageMiddleware.d_6.MoveNext()
删除并重新创建数据库。。。非常明显。
//已解决。
3年后,我研究了这个问题。重新创建数据库没有帮助。如上所述,我已将PK
从string
更改为int
,并且UserManager
已成功创建记录,但随后SignInManager
未能以相同错误登录新创建的用户。
解决方案:
我发现,只需在创建时为用户分配一个角色就可以解决问题。
await userManager.AddToRoleAsync(user, UserRoles.Basic.ToString());