locked
Microsoft.AspNet.UserManager - is there a synchronous version of 'CreateAsync' - how do I add multiple users in a single shot? RRS feed

  • Question

  • User-1851576699 posted

    I am trying to add multiple ApplicationUser types to the 'AspNetUsers' table in my MVC project but there is only an async version of method for adding new users via the UserManger<TUser> type. Where is the synchronous version for adding multiple users in a single shot? I am trying to do this in my override of the Seed() method for my custom database initialization strategy.

    I tried calling .CreateAsync(); by putting it in a foreach() loop and just ignoring the return value but this doesn't work. Is there a corresponding version of .CreateAsync() that isn't asynchronous?

    EDIT: If it's not possible to add multiple ApplicationUser types in a single shot then my next plan of action is to do it myself without the help of .CreateAsync or a UserManager wrapper object but I'll need to know how to hash the ApplicationUser passwords and also get a security stamp. What two methods would I call to get those field values. Are they public methods that I can even call?

    Tuesday, April 22, 2014 8:32 PM

Answers

  • User455156504 posted

    Alright, a valid opinion :-) According to the documentation the security stamp is a random string value, so you can put in any string value in there. I haven't done it yet, but you could generate a random int value and parse it into a string and store it as your security stamp. With the next change of the user this value will be overwritten.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 28, 2014 6:18 AM

All replies

  • User455156504 posted

    Hi,

    I had a similar problem in one of my projects. One option would be to force the asynchronous methods to run synchronously via .Result. Another option is the one you already mentioned, adding it by hand via the underlying objects. For hashing the passwords you should use the PasswordHasher class. The security stamp must not be set in my opinion, because it is only relevant if you're updating an existing object (what doesn't seem to be the case in your scenario).

    Wednesday, April 23, 2014 8:48 AM
  • User-1851576699 posted

    Thank you for your response. I tried adding multiple users using the UserManager.CreateAsync() method but I don't have enough proper knowledge about async methods to successfully do so. So I am trying it by hand.

    I have been able to successfully use the PasswordHasher class to generate hashes for all my seed ApplicationUsers.

    You state that the security stamp must not be set in your opinion. I have a different opinion. :) I keep getting this exception because my virgin users security stamp is set to null. This occurs when trying to login through the /Account/Login method.

    Server Error in '/' Application.


    Value cannot be null.
    Parameter name: value

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.ArgumentNullException: Value cannot be null.
    Parameter name: value

    Source Error: 

    Line 431:        {
    Line 432:            AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
    Line 433:            var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
    Line 434:            AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
    Line 435:        

    Inserting a new ApplicationUser is a 'change' in some sense is it not? If you could point me to the API object that generates the security stamp that would be great. I'll ook for it in the meantime. 

    Saturday, April 26, 2014 8:53 AM
  • User455156504 posted

    Alright, a valid opinion :-) According to the documentation the security stamp is a random string value, so you can put in any string value in there. I haven't done it yet, but you could generate a random int value and parse it into a string and store it as your security stamp. With the next change of the user this value will be overwritten.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, April 28, 2014 6:18 AM