示例#1
0
        /// <summary>
        /// Populate MongoDB with a List of Dummy users to enable tests - e.g. Alice, Bob
        ///   see Config.GetSampleUsers() for details.
        /// </summary>
        /// <param name="userManager"></param>
        private static void  AddSampleUsersToMongo(UserManager <Microsoft.AspNetCore.Identity.MongoDB.IdentityUser> userManager)
        {
            var dummyUsers = Config.GetUsers();

            foreach (var usrDummy in dummyUsers)
            {
                var user = new Microsoft.AspNetCore.Identity.MongoDB.IdentityUser()
                {
                    UserName        = usrDummy.Username,
                    LockoutEnabled  = false,
                    EmailConfirmed  = true,
                    Email           = "*****@*****.**",
                    NormalizedEmail = "*****@*****.**"
                };



                foreach (var claim in usrDummy.Claims)
                {
                    user.AddClaim(claim);
                }
                var result = userManager.CreateAsync(user, usrDummy.Password);
                if (!result.Result.Succeeded)
                {
                    // If we got an error, Make sure to drop all collections from Mongo before trying again. Otherwise sample users will NOT be populated
                    var errorList = result.Result.Errors.ToArray();
                    throw new Exception($"Error Adding sample users to MongoDB! Make sure to drop all collections from Mongo before trying again!");
                }
            }
            return;
        }
        private async Task LoadSharedKeyAndQrCodeUriAsync(Microsoft.AspNetCore.Identity.MongoDB.IdentityUser user, EnableAuthenticatorViewModel model)
        {
            var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);

            if (string.IsNullOrEmpty(unformattedKey))
            {
                await _userManager.ResetAuthenticatorKeyAsync(user);

                unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);
            }

            model.SharedKey        = FormatKey(unformattedKey);
            model.AuthenticatorUri = GenerateQrCodeUri(user.Email, unformattedKey);
        }