Creating a mass user in OwinContext (performance)

I upload an excel file containing all the necessary users to my site using ASP.NET Identity and OwinContext and EF 6. My code looks like this:

foreach (var bulkUserDetail in bulkUser.BulkUserDetails)
{

        var userManager = owinContext.GetUserManager<ApplicationUserManager>();

        var userProfile = new UserProfile();

        userProfile.Username = bulkUserDetail.Username;


        AspNetUser newUser = new AspNetUser
        {
            UserName = userProfile.Username,
            Email = bulkUserDetail.Email,
            LastPasswordChangedDate = null,
        };

        var creationResult = userManager.Create(newUser);

        if (creationResult.Succeeded)
        {

            string token = userManager.GeneratePasswordResetToken(newUser.Id);

        }

}

The problem is that the performance of the next two lines is pretty disappointing

userManager.Create(newUser) -- (900 milliseconds)
userManager.GeneratePasswordResetToken(newUser.Id)      --(1800 milliseconds)

In large numbers, that is, 2,000 users, performance is becoming a serious problem. Is it better practice to speed up this process? I am open to suggestions, but I need to leave the OwinContext library.

Thanks in advance

+4
source share
1 answer

You can try making user creation inside parallel, which can speed up the overall time, however there is a problem with this:

  • Create GeneratePasswordResetToken ,
  • , , , .

    var userManager = owinContext.GetUserManager<ApplicationUserManager>();
    
    Parallel.ForEach (bulkUser.BulkUserDetails, bulkUserDetail => 
    {       
        //Do you really need to make this userProfile as its not used
        var userProfile = new UserProfile();
    
        userProfile.Username = bulkUserDetail.Username;
    
        AspNetUser newUser = new AspNetUser
        {
            UserName = userProfile.Username,
            Email = bulkUserDetail.Email,
            LastPasswordChangedDate = null,
        };
    
        var creationResult = userManager.Create(newUser);
    
        if (creationResult.Succeeded)
        {
    
            string token = userManager.GeneratePasswordResetToken(newUser.Id);
    
        }
    })
    
0

All Articles