值不能为空.参数名称:value, CreateIdentityAsync



我创建了一个ViewModel(UserModel)来实现IUser<int>(用于自定义ASP。. NET Identity 2.0)

public class UserModel : IUser<int>
{
    public int Id { get; set; }
    public string SecurityStamp { get; set; }
    [Display(Name = "Name")]
    public string FirstName { get; set; }
    [Display(Name = "Last Name")]
    public string LastName { get; set; }
    public string FullName { get; set; }
    [Display(Name = "Username")]
    public string UserName { get; set; }
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }
    [Display(Name = "E-Mail")]
    public string Email { get; set; }
    [Display(Name = "Photo")]
    public string PhotoPath { get; set; }
    public List<Claim> Claims { get; set; }
    [IgnoreMapping]
    public List<CommentModel> Comments { get; set; }
}

一切都好,但GenerateUserIdentityAsync方法。
以下代码是我在UserModel中的GenerateUserIdentityAsync:

public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<UserModel, int> manager)
{
    this.SecurityStamp = Guid.NewGuid().ToString();
    this.Claims = new List<Claim>();
    ClaimsIdentity userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);//Exception : Value cannot be null. Parameter name: value
    return userIdentity;
}

CreateIdentityAsync方法中发生了一个异常,下面的堆栈跟踪

ArgumentNullException: Value cannot be null.
Parameter name: value]
   System.Security.Claims.Claim..ctor(String type, String value, String valueType, String issuer, String originalIssuer, ClaimsIdentity subject, String propertyKey, String propertyValue) +10798181
   System.Security.Claims.Claim..ctor(String type, String value) +34
   Microsoft.AspNet.Identity.<CreateAsync>d__0.MoveNext() +1458
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24
   Smartiz.UI.Models.<GenerateUserIdentityAsync>d__2.MoveNext() in e:WorksSmartizSmartiz.UIModelsUserModels.cs:215
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +24
   Smartiz.UI.Controllers.<SignInAsync>d__58.MoveNext() in e:WorksSmartizSmartiz.UIControllersAccountController.cs:448
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21
   Smartiz.UI.Controllers.<ExternalLoginCallback>d__3c.MoveNext() in e:WorksSmartizSmartiz.UIControllersAccountController.cs:315
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
   System.Runtime.CompilerServices.TaskAwaiter.GetResult() +21
   System.Threading.Tasks.TaskHelpersExtensions.ThrowIfFaulted(Task task) +61
   System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +114
   System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult) +66
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +47
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +49
   System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +117
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +323
   System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +44
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +47
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +136
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +50
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +72
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +185
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +133
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +34
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44
   System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +39
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +62
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +39
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +70
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +139
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9651188
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

你能告诉我我错在哪里吗?

我在从ASP升级时遇到了同样的问题。. NET Identity 1到ASP。我通过在SecurityStamp字段中放入一个随机字符串来解决这个问题。

我在这里找到了这个解决方案。

正如Sam在他之前的回答中提到的,我对这个问题的解决方案是确保我在EF迁移的Seed方法中创建的用户在SecurityStamp中有一些东西。一旦我这样做了,并做了一个update-database -force,我就可以很好地登录了。

context.Users.AddOrUpdate(u => u.UserName,
                new ApplicationUser
                {
                    UserName = "demo",
                    Email = "demo@demo.com",
                    EmailConfirmed = true,
                    PhoneNumberConfirmed = true,
                    PasswordHash = password,
                    PhoneNumber = "111-222-3344",
                    SecurityStamp = Guid.NewGuid().ToString() //THIS IS WHAT I NEEDED
                });

如果您通过其他方式创建了您的用户,您应该能够通过sql在users db列中添加字符串来修复它,并获得相同的成功。

与Kevin的情况类似,我们也遇到了这个问题。原来,在播种数据库时,有人忘记设置UserName属性,从而产生了相同的错误。

我有一个更普遍的问题-在sql升级例程运行后,我的一些声明在AspNetUserClaims表中具有空值。

我通过将ClaimValues设置为空字符串来解决这个问题。

对我来说,它是重写UserName字段,以便使用我自己的Display属性。我在ApplicationUser类中使用以下内容:

[Display(Name = "Whatever")]
public new string UserName { get; set; }

当我删除这个,登录工作如预期。我也覆盖了EmailPhoneNumber,但UserName导致了这个问题。

这个问题让我在办公室里折腾了好几个小时。

这是导致问题的行:

result = signinManager.PasswordSignIn(txtthecrmusername.Text, txtthecrmpassword.Text, RememberMe.Checked, shouldLockout:=False)

最后,我在AspNetUsers中添加了一个自定义字段,并将其保留为空。在匆忙中,我还忘记在创建用户时将其设置为none。

自定义用户声明不能为空,但标准声明可以为空。

我遇到了SignInManager无法登录新创建的用户,出现了相同的错误。

解决方案:

我发现只要在创建时为用户分配一个角色就可以解决问题。希望它能帮助到别人。

await userManager.AddToRoleAsync(user, UserRoles.Basic.ToString());

对于其他人来说,来到这里后搜索的原因"值不能为空。参数名称:value"没有"CreateIdentityAsync"问题可能是DataAnnotations中的空值,例如:[Display(Name = "")]

此错误的原因是由于验证属性中的一个空值

[Display(Name = " ")]

相关内容

  • 没有找到相关文章

最新更新