53

I created a ViewModel(UserModel) that implement IUser<int> (for customizing 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; }
}

Everything is well, but GenerateUserIdentityAsync method.
The following code is my GenerateUserIdentityAsync in UserModel:

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;
}

An exception has occurred in CreateIdentityAsync method with the following stack trace

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

Could you please guide me where my wrong is ?

Mohammad Dayyan
  • 21,578
  • 41
  • 164
  • 232

9 Answers9

107

I faced the same issue while upgrading from ASP.NET Identity 1 to ASP.NET Identity 2 and I solved it by putting a random string in the field SecurityStamp.

I found this solution here.

I hope it works for you too.

Pokechu22
  • 4,984
  • 9
  • 37
  • 62
user2536835
  • 1,086
  • 1
  • 8
  • 4
  • 4
    Do you guys know what else it could be? I tried using a guid and putting it as the securitystamp but I keep getting this damn exception anyway. – ObedMarsh Nov 20 '14 at 13:53
  • 6
    Thank you for this. I was banging my head against the wall trying to figure out why the `SignInManager` wasn't working correctly. Turned out that I created some users before I implemented `IUserSecurityStampStore` so the `SecurityStamp` was null. When you try to log in it takes the `SecurityStamp` and stuffs it into the `ClaimsIdentity`. Apparently it chokes when `Claim.Value` is null. – Sam Mar 06 '15 at 18:23
  • 1
    I struggled with this for most of the day, and it turns out I had two problems. One was the aforementioned SecurityStamp field. The second, and the one that gave me so much grief, was that I had a null in the list of the user's roles. This is an unlikely occurrence in most situations, but my particular configuration made it possible and checking for it and removing the null when loading the set of roles fixed the problem. – Aaron Averett Dec 07 '16 at 22:49
  • What is a SecurityStamp and how do I populate it? – Matt May 22 '17 at 18:15
  • Worked for me. I was missing security stamp value and was null. When i update the value it started working again – Atta H. Aug 14 '18 at 17:19
  • Gold. Had the same issue for seeded users. Thank you! – Andrei Apr 21 '19 at 05:52
37

As Sam mentioned in his comment for the previous answer, my solution to this problem was to make sure that the user I was creating in in the Seed method of the EF Migration had something in the SecurityStamp. Once I did that and did a update-database -force, I was able to log in just fine.

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
                });

If you created your user through some other means, you should be able to fix it by putting a string in the users db column via sql and have the same success.

Kevin Heidt
  • 1,135
  • 1
  • 11
  • 14
4

In a similar way to Kevin, we were also experiencing this issue. It turns out that when seeding the database someone had forgotten to set the UserName property which produced this same error.

Barrie
  • 1,444
  • 19
  • 26
3

I had a more generic issue - some of my claims had Null values in the AspNetUserClaims table after a sql upgrade routine had run.

I solved the issue by setting the ClaimValues to an empty string.

Liam
  • 5,033
  • 2
  • 30
  • 39
  • I had this issue, too. In my case it simply was caused by adding `RoleClaim`s or `UserClaim`s via the context with only a `ClaimType`, but without the `ClaimValue`. – FranzHuber23 Feb 25 '19 at 10:56
2

For me it was overriding UserName field, in order to use my own Display attribute. I was using the following in my ApplicationUser class:

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

When I remove this, signin works as expected. I have also overriden Email and PhoneNumber but UserName causes the issue.

Medise
  • 351
  • 1
  • 2
  • 11
0

This problem kicked me around the office for a few hours.

This was the line causing the problem:

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

In the end, I'd added a custom field to AspNetUsers and left it null. In my haste I'd also forgotten to set it to nothing when creating users.

Custom user claims cannot be null but standard claims can.

Deadeye
  • 51
  • 4
0

I have stucked with the SignInManager failing to sign in the newly created user with the same error.

Solution:

I have found out that just assiging a role to the user upon creation solved the issue. Hope it helps somebody else.

await userManager.AddToRoleAsync(user, UserRoles.Basic.ToString());
Kia Kaha
  • 1,565
  • 1
  • 17
  • 39
0

For others who came here after searching for the cause of "Value cannot be null. Parameter name: value" without "CreateIdentityAsync" error, The problem may be a null value in DataAnnotations like: [Display(Name = "")]

user2903753
  • 153
  • 2
  • 12
0

the reason for this error is due to the empty value in on of the validation attributes

[Display(Name = " ")]
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
  • It would be useful to give more context and justification to your answer (i.e. references, explanation, etc.) – Daileyo May 13 '21 at 23:08