public async Task FindByEmail()
        {
            using (var store = new UserStoreFixture <ElasticUser>())
            {
                var user = new ElasticUser(UserId, UserName)
                {
                    Email = new ElasticUserEmail
                    {
                        Address     = "*****@*****.**",
                        IsConfirmed = false
                    }
                };

                await store.CreateAsync(user);

                var elasticUser = await store.FindByEmailAsync(user.Email.Address);

                Assert.NotNull(elasticUser);
                Assert.Equal(user.EmailAddress, elasticUser.EmailAddress);

                // should ignore case
                elasticUser = await store.FindByEmailAsync(user.Email.Address.ToUpper());

                Assert.NotNull(elasticUser);
                Assert.Equal(user.EmailAddress, elasticUser.EmailAddress);
            }
        }
Пример #2
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                var user = new ElasticUser {
                    UserName = model.Email, Email = new ElasticConfirmation(model.Email)
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=532713
                    // Send an email with this link
                    //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                    //var callbackUrl = Url.Action(nameof(ConfirmEmail), "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
                    //await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
                    //    $"Please confirm your account by clicking this link: <a href='{callbackUrl}'>link</a>");
                    await _signInManager.SignInAsync(user, isPersistent : false);

                    _logger.LogInformation(3, "User created a new account with password.");
                    return(RedirectToLocal(returnUrl));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        public async Task AddLogin_ShouldNotAdd_Test()
        {
            var store = new ElasticUserStore <ElasticUser, ElasticRole>(_nestClient, new ElasticOptions {
                UsersIndex = _userIndex
            });

            var user = new ElasticUser
            {
                UserName     = "******",
                PasswordHash = "phash",
                Logins       = new List <ElasticUserLogin> {
                    new ElasticUserLogin(new UserLoginInfo("prov1", "key1", "test1"))
                },
                Email = new ElasticConfirmation("*****@*****.**")
            };

            var createResult = await store.CreateAsync(user, NoCancellationToken);

            await store.AddLoginAsync(user, new UserLoginInfo("prov1", "key1", "test1"), NoCancellationToken);

            var elasticUser = await store.FindByIdAsync(user.Id, NoCancellationToken);

            Assert.Equal(createResult, IdentityResult.Success);
            Assert.Equal(user.Logins.Count, 1);
        }
Пример #4
0
        public async Task CustomTypeName()
        {
            const string indexName  = "some-index";
            const string entityName = "world";

            try {
                var userStore = new ElasticUserStore <ElasticUser>(
                    _connectionString,
                    indexName: indexName,
                    entityName: entityName,
                    forceRecreate: true
                    );

                var user = new ElasticUser {
                    UserName = "******"
                };

                user.Roles.UnionWith(new[] { "hello" });

                var userManager = new UserManager <ElasticUser>(userStore);
                AssertIdentityResult(await userManager.CreateAsync(user, "some password"));


                var response = Client.Get <ElasticUser>(user.Id, indexName, entityName);
                Assert.That(response.Source, Is.Not.Null);
                Assert.That(response.Source.UserName, Is.EqualTo(user.UserName));
            }
            finally {
                Client.DeleteIndex(i => i.Index(indexName));
            }
        }
        public async Task FindByLogin_Test()
        {
            var store = new ElasticUserStore <ElasticUser, ElasticRole>(_nestClient, new ElasticOptions {
                UsersIndex = _userIndex
            });

            var user1 = new ElasticUser
            {
                UserName     = "******",
                PasswordHash = "phash",
                Logins       = new List <ElasticUserLogin> {
                    new ElasticUserLogin(new UserLoginInfo("prov1", "key1", "test1"))
                },
                Email = new ElasticConfirmation("*****@*****.**")
            };
            var user2 = new ElasticUser
            {
                UserName     = "******",
                PasswordHash = "phash",
                Logins       = new List <ElasticUserLogin> {
                    new ElasticUserLogin(new UserLoginInfo("prov2", "key2", "test2"))
                },
                Email = new ElasticConfirmation("*****@*****.**")
            };

            var createResult1 = await store.CreateAsync(user1, NoCancellationToken);

            var createResult2 = await store.CreateAsync(user2, NoCancellationToken);

            var elasticUser = await store.FindByLoginAsync("prov2", "key2", NoCancellationToken);

            Assert.Equal(createResult1, IdentityResult.Success);
            Assert.Equal(createResult2, IdentityResult.Success);
            Assert.Equal(user2.Id, elasticUser.Id);
        }
        public async Task RemoveClaim_Test()
        {
            var store = new ElasticUserStore <ElasticUser, ElasticRole>(_nestClient, new ElasticOptions {
                UsersIndex = _userIndex
            });

            var user = new ElasticUser
            {
                UserName     = "******",
                PasswordHash = "phash",
                Logins       = new List <ElasticUserLogin> {
                    new ElasticUserLogin(new UserLoginInfo("prov1", "key1", "test1"))
                },
                Claims = new List <ElasticClaim> {
                    new ElasticClaim("type1", "value1"),
                    new ElasticClaim("type2", "value2")
                },
                Email = new ElasticConfirmation("*****@*****.**")
            };

            var createResult = await store.CreateAsync(user, NoCancellationToken);

            var claimsToBeRemoved = new List <Claim>
            {
                new Claim("type2", "value2")
            };

            await store.RemoveClaimsAsync(user, claimsToBeRemoved, NoCancellationToken);

            var elasticUser = await store.FindByIdAsync(user.Id, NoCancellationToken);

            Assert.Equal(createResult, IdentityResult.Success);
            Assert.Equal(elasticUser.Claims.Count, 2);
        }
        public async Task SetTwoFactorEnabled_Test()
        {
            var store = new ElasticUserStore <ElasticUser, ElasticRole>(_nestClient, new ElasticOptions {
                UsersIndex = _userIndex
            });

            var user = new ElasticUser
            {
                UserName     = "******",
                PasswordHash = "phash",
                Logins       = new List <ElasticUserLogin> {
                    new ElasticUserLogin(new UserLoginInfo("prov1", "key1", "test1"))
                },
                Claims = new List <ElasticClaim> {
                    new ElasticClaim("type", "value1")
                },
                Email = new ElasticConfirmation("*****@*****.**")
            };

            var createResult = await store.CreateAsync(user, NoCancellationToken);

            await store.SetTwoFactorEnabledAsync(user, true, NoCancellationToken);

            await store.UpdateAsync(user, NoCancellationToken);

            var elasticUser = await store.FindByIdAsync(user.Id, NoCancellationToken);

            Assert.Equal(createResult, IdentityResult.Success);
            Assert.True(elasticUser.IsTwoFactorEnabled);
        }
 private void Map(ElasticUser euser, User user)
 {
     user.Id                  = euser.Id;
     user.UserName            = euser.UserName;
     user.PasswordHash        = euser.PasswordHash;
     user.RegistrationDateUtc = euser.RegistrationDateUtc;
 }
 private void Map(User user, ElasticUser euser)
 {
     euser.Id                  = user.Id;
     euser.UserName            = user.UserName;
     euser.PasswordHash        = user.PasswordHash;
     euser.RegistrationDateUtc = user.RegistrationDateUtc;
 }
        public async Task CreateUserWithElasticId()
        {
            using (var store = new UserStoreFixture <ElasticUser>())
            {
                var user = new ElasticUser()
                {
                    UserName = UserName,
                    Phone    = new ElasticUserPhone
                    {
                        Number      = "555 123 1234",
                        IsConfirmed = true
                    },
                    Email = new ElasticUserEmail
                    {
                        Address     = "*****@*****.**",
                        IsConfirmed = false
                    }
                };

                await store.CreateAsync(user);

                var elasticUser = await store.FindByNameAsync(UserName);

                Assert.NotNull(elasticUser);
                Assert.Equal(elasticUser.UserName, UserName);
                Assert.NotNull(elasticUser.Id);
                Assert.NotEqual(UserId, elasticUser.Id);
                Assert.NotNull(elasticUser.Version);
            }
        }
Пример #11
0
        public async Task <IActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl = null)
        {
            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await _signInManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    return(View("ExternalLoginFailure"));
                }
                var user = new ElasticUser {
                    UserName = model.Email, Email = new ElasticConfirmation(model.Email)
                };
                var result = await _userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await _userManager.AddLoginAsync(user, info);

                    if (result.Succeeded)
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        _logger.LogInformation(6, "User created an account using {Name} provider.", info.LoginProvider);
                        return(RedirectToLocal(returnUrl));
                    }
                }
                AddErrors(result);
            }

            ViewData["ReturnUrl"] = returnUrl;
            return(View(model));
        }
Пример #12
0
        public ElasticResult <ElasticUser> Update(ElasticUser user)
        {
            var response = _elasticRepository.ExecuteCreateOrUpdateRequest(user, EsType);

            return(response.Success
                ? ElasticResult <ElasticUser> .SuccessResult(user)
                : ElasticResult <ElasticUser> .FailResult(response.Message));
        }
        public async Task CreateAsync(User user)
        {
            if (await FindByNameAsync(user.UserName) != null)
            {
                throw new ArgumentException(string.Format("Username: '******' is already taken.", user.UserName));
            }
            var euser = new ElasticUser();

            Map(user, euser);
            await eclient.IndexAsync(euser, ind => ind.Index(AppUsersIndexName));
        }
Пример #14
0
        public async Task FindById()
        {
            var store = CreateStore();
            var user  = new ElasticUser(UserName);

            await store.CreateAsync(user);

            var elasticUser = await store.FindByIdAsync(user.Id);

            Assert.IsNotNull(elasticUser);
            Assert.AreEqual(user.Id, elasticUser.Id);
        }
        public User Convert(ElasticUser eUser, GameSetupInfo setupInfo)
        {
            var user = new User
            {
                Id               = eUser.UserId,
                MaxHandsPerDay   = CalculateMaxHandsPerDay(setupInfo),
                MaxVisitsPerWeek = CalculateMaxVisitsPerWeek(setupInfo),
                Name             = eUser.Name,
            };

            return(user);
        }
Пример #16
0
        public async Task DeleteUser()
        {
            var store = CreateStore();
            var user  = new ElasticUser(UserName);

            await store.CreateAsync(user);

            await store.DeleteAsync(user);

            user = await store.FindByNameAsync(user.UserName);

            Assert.That(user, Is.Null);
        }
Пример #17
0
        public async Task GetUsersInRole_Test()
        {
            var store = new ElasticUserStore <ElasticUser, ElasticRole>(_nestClient, new ElasticOptions {
                UsersIndex = _userIndex
            });

            var user1 = new ElasticUser
            {
                UserName     = "******",
                PasswordHash = "phash",
                Logins       = new List <ElasticUserLogin> {
                    new ElasticUserLogin(new UserLoginInfo("prov1", "key1", "test1"))
                },
                Claims = new List <ElasticClaim> {
                    new ElasticClaim("type", "value1")
                },
                Email = new ElasticConfirmation("*****@*****.**")
            };
            var user2 = new ElasticUser
            {
                UserName     = "******",
                PasswordHash = "phash",
                Logins       = new List <ElasticUserLogin> {
                    new ElasticUserLogin(new UserLoginInfo("prov2", "key2", "test2"))
                },
                Claims = new List <ElasticClaim> {
                    new ElasticClaim("type", "value2")
                },
                Email = new ElasticConfirmation("*****@*****.**")
            };

            var createResult1 = await store.CreateAsync(user1, NoCancellationToken);

            var createResult2 = await store.CreateAsync(user2, NoCancellationToken);

            await store.AddToRoleAsync(user1, "role1", NoCancellationToken);

            await store.AddToRoleAsync(user1, "role2", NoCancellationToken);

            await store.AddToRoleAsync(user2, "role1", NoCancellationToken);

            var usersForRole1 = await store.GetUsersInRoleAsync("role1", NoCancellationToken);

            var usersForRole2 = await store.GetUsersInRoleAsync("role2", NoCancellationToken);

            Assert.Equal(createResult1, IdentityResult.Success);
            Assert.Equal(createResult2, IdentityResult.Success);
            Assert.Equal(usersForRole1.Count, 2);
            Assert.Equal(usersForRole2.Count, 1);
        }
        public async Task FindById()
        {
            using (var store = new UserStoreFixture <ElasticUser>())
            {
                var user = new ElasticUser(UserId, UserName);

                await store.CreateAsync(user);

                var elasticUser = await store.FindByIdAsync(user.Id);

                Assert.NotNull(elasticUser);
                Assert.Equal(user.Id, elasticUser.Id);
            }
        }
        public async Task DeleteUser()
        {
            using (var store = new UserStoreFixture <ElasticUser>())
            {
                var user = new ElasticUser(UserId, UserName);

                await store.CreateAsync(user);

                await store.DeleteAsync(user);

                var elasticUser = await store.FindByNameAsync(user.UserName);

                Assert.Null(elasticUser);
            }
        }
Пример #20
0
        public async Task Create_Test()
        {
            var store = new ElasticUserStore <ElasticUser, ElasticRole>(_nestClient, new ElasticOptions {
                UsersIndex = _userIndex
            });

            var user = new ElasticUser
            {
                UserName     = "******",
                PasswordHash = "phash",
                Email        = new ElasticConfirmation("*****@*****.**")
            };

            var result = await store.CreateAsync(user, NoCancellationToken);

            Assert.Equal(result, IdentityResult.Success);
        }
Пример #21
0
        public async Task UpdateUser()
        {
            var store = CreateStore();
            var user  = new ElasticUser(UserName);

            await store.CreateAsync(user);

            user = await store.FindByIdAsync(user.Id);

            user.Roles.Add("hello");

            await store.UpdateAsync(user);

            user = await store.FindByIdAsync(user.Id);

            Assert.That(user.Roles, Contains.Item("hello"));
        }
Пример #22
0
        public async Task FindByEmail()
        {
            var store = CreateStore();
            var user  = new ElasticUser(UserName)
            {
                Email = new ElasticUserEmail {
                    Address     = "*****@*****.**",
                    IsConfirmed = false
                }
            };

            await store.CreateAsync(user);

            var elasticUser = await store.FindByEmailAsync(user.Email.Address);

            Assert.IsNotNull(elasticUser);
            Assert.AreEqual(user.EmailAddress, elasticUser.EmailAddress);
        }
Пример #23
0
        public async Task GetLogins_Test()
        {
            var store = new ElasticUserStore <ElasticUser, ElasticRole>(_nestClient, new ElasticOptions {
                UsersIndex = _userIndex
            });

            var user = new ElasticUser
            {
                UserName     = "******",
                PasswordHash = "phash",
                Logins       = new List <ElasticUserLogin> {
                    new ElasticUserLogin(new UserLoginInfo("prov1", "key", "test1")),
                    new ElasticUserLogin(new UserLoginInfo("prov2", "key2", "test1"))
                },
                Email = new ElasticConfirmation("*****@*****.**")
            };

            var logins = await store.GetLoginsAsync(user, NoCancellationToken);

            Assert.Equal(2, logins.Count);
        }
Пример #24
0
        public async Task GetNormalizedUserName_Test()
        {
            var store = new ElasticUserStore <ElasticUser, ElasticRole>(_nestClient, new ElasticOptions {
                UsersIndex = _userIndex
            });

            const string expected = "test1";
            var          user     = new ElasticUser
            {
                UserName     = "******",
                PasswordHash = "phash",
                Logins       = new List <ElasticUserLogin> {
                    new ElasticUserLogin(new UserLoginInfo("prov1", "key", "test1")),
                    new ElasticUserLogin(new UserLoginInfo("prov2", "key2", "test1"))
                },
                Email = new ElasticConfirmation("*****@*****.**")
            };
            var actual = await store.GetNormalizedUserNameAsync(user, NoCancellationToken);

            Assert.Equal(expected, actual);
        }
Пример #25
0
        public async Task CreateUser()
        {
            var store = CreateStore();
            var user  = new ElasticUser(UserName)
            {
                Phone = new ElasticUserPhone {
                    Number      = "555 123 1234",
                    IsConfirmed = true
                },
                Email = new ElasticUserEmail {
                    Address     = "*****@*****.**",
                    IsConfirmed = false
                }
            };

            await store.CreateAsync(user);

            user = await store.FindByNameAsync(UserName);

            Assert.That(user, Is.Not.Null);
            Assert.That(user.UserName, Is.EqualTo(UserName));
        }
        public async Task UpdateUser()
        {
            using (var store = new UserStoreFixture <ElasticUser>())
            {
                var user = new ElasticUser(UserId, UserName);

                await store.CreateAsync(user);

                user = await store.FindByIdAsync(user.Id);

                user.Roles.Add("hello");

                await store.UpdateAsync(user);

                user = await store.FindByIdAsync(user.Id);

                Assert.True(user.Roles.Contains("hello"));

                // create another user object from the same id
                var sameUser = await store.FindByIdAsync(user.Id);

                sameUser.Roles.Add("another_role");
                await store.UpdateAsync(sameUser);

                sameUser = await store.FindByIdAsync(sameUser.Id);

                Assert.True(sameUser.Roles.Contains("another_role"));

                // same id, different versions
                Assert.Equal(user.Id, sameUser.Id);
                Assert.NotEqual(user.Version, sameUser.Version);

                // exception should be thrown as we're attempting to
                // update the original, out of date, user.
                user.Roles.Add("bad_role");
                await Assert.ThrowsAsync <Elasticsearch.Net.ElasticsearchClientException>(async() => await store.UpdateAsync(user));
            }
        }
        public async Task FailToCreateUserDueToExistingId()
        {
            using (var store = new UserStoreFixture <ElasticUser>())
            {
                var user = new ElasticUser(UserId, UserName);

                await store.CreateAsync(user);

                bool threwConflictException = false;
                try
                {
                    await store.CreateAsync(user);
                }
                catch (ElasticsearchClientException ex)
                {
                    if (ex.Message.Contains("409"))
                    {
                        threwConflictException = true;
                    }
                }

                Assert.True(threwConflictException);
            }
        }
Пример #28
0
        public ElasticResult <ElasticUser> Add(string login, string password)
        {
            var user = new ElasticUser(login, password);

            return(_entityRepository.Add(EsType, user));
        }
Пример #29
0
        public ElasticResult <ElasticMessage> AddAdminMessage(string chatGuid, ElasticUser user, string text)
        {
            var message = new ElasticMessage(chatGuid, user.Guid, user.UserName, text, true);

            return(_entityRepository.Add(EsType, message));
        }