【问题标题】:Value cannot be null. Parameter name: value, CreateIdentityAsync?值不能为空。参数名称:值,CreateIdentityAsync?
【发布时间】:2014-07-28 08:30:34
【问题描述】:

我创建了一个实现 IUser<int> 的 ViewModel(UserModel)(用于自定义 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:\Works\Smartiz\Smartiz.UI\Models\UserModels.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:\Works\Smartiz\Smartiz.UI\Controllers\AccountController.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:\Works\Smartiz\Smartiz.UI\Controllers\AccountController.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

你能指导我哪里错了吗?

【问题讨论】:

  • 你能展示一下 CreateIdentityAsync() 方法吗?
  • 这可能会有所帮助:stackoverflow.com/questions/21918000/…
  • @bit:我不会在任何地方实现CreateIdentityAsync()。我应该实施它吗?
  • [IgnoreMap] 顺便说一句

标签: c# asp.net-mvc customization asp.net-identity


【解决方案1】:

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

我找到了这个解决方案here

我希望它也对你有用。

【讨论】:

  • 谢谢 - 也解决了我的问题!
  • 你们知道还有什么可能吗?我尝试使用 guid 并将其作为安全标记,但无论如何我都会收到这个该死的异常。
  • 谢谢你。我正用头撞墙,试图弄清楚为什么SignInManager 不能正常工作。原来我在实现IUserSecurityStampStore 之前创建了一些用户,所以SecurityStamp 为空。当您尝试登录时,它会将SecurityStamp 填入ClaimsIdentity。显然,当Claim.Value 为空时它会窒息。
  • 我整天都在为此苦苦挣扎,结果发现我遇到了两个问题。一个是前面提到的 SecurityStamp 字段。第二个,也是让我非常伤心的一个,是我在用户角色列表中有一个空值。在大多数情况下这不太可能发生,但我的特定配置使其成为可能,并在加载角色集时检查它并删除 null 解决了问题。
  • 我怎么亲你?这只是救了我。完全不清楚你应该用 eandom 字符串填充这个字段。例外也无助于理解它。非常感谢。
【解决方案2】:

正如 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 列中放置一个字符串来修复它并获得同样的成功。

【讨论】:

    【解决方案3】:

    与 Kevin 类似,我们也遇到了这个问题。事实证明,在为数据库播种时,有人忘记设置 UserName 属性,从而产生了同样的错误。

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

      • 我也有这个问题。在我的情况下,它只是通过上下文添加RoleClaims 或UserClaims 引起的,只有ClaimType,但没有ClaimValue
      【解决方案5】:

      对我来说,它覆盖了UserName 字段,以便使用我自己的Display 属性。我在ApplicationUser 课程中使用了以下内容:

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

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

      【讨论】:

        【解决方案6】:

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

        这是导致问题的行:

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

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

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

        【讨论】:

          【解决方案7】:

          我一直坚持SignInManager 无法登录新创建的用户并出现同样的错误。

          解决方案:

          我发现只需在创建时将角色分配给用户即可解决问题。希望它可以帮助其他人。

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

          【讨论】:

            【解决方案8】:

            对于其他人在搜索“Value cannot be null.Parameter name: value”没有“CreateIdentityAsync”错误的原因后来到这里,问题可能是DataAnnotations中的空值如:[Display(Name = "")]

            【讨论】:

              【解决方案9】:

              这个错误的原因是验证属性中的空值

              [Display(Name = " ")]
              

              【讨论】:

              • 为您的答案提供更多背景和理由(即参考、解释等)会很有用
              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-03-21
              • 2016-01-16
              • 2014-05-17
              • 2016-05-14
              • 2015-11-05
              相关资源
              最近更新 更多