コード例 #1
0
        public async Task <TUser> FindAsync(UserLoginInfo login)
        {
            string loginId = Util.GetLoginId(login);

            var loginDoc = await session.Include <IdentityUserLogin>(x => x.UserId)
                           .LoadAsync(loginId);

            if (loginDoc != null)
            {
                return(await session.LoadAsync <TUser>(loginDoc.UserId));
            }

            return(null);
        }
コード例 #2
0
        public async Task RemoveLoginAsync(TUser user, UserLoginInfo login)
        {
            ThrowIfDisposed();
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            string loginId  = Util.GetLoginId(login);
            var    loginDoc = await session.LoadAsync <IdentityUserLogin>(loginId);

            if (loginDoc != null)
            {
                session.Delete(loginDoc);
            }

            user.Logins.RemoveAll(x => x.LoginProvider == login.LoginProvider && x.ProviderKey == login.ProviderKey);
        }
コード例 #3
0
        public async Task AddLoginAsync(TUser user, UserLoginInfo login)
        {
            ThrowIfDisposed();
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            if (!user.Logins.Any(x => x.LoginProvider == login.LoginProvider && x.ProviderKey == login.ProviderKey))
            {
                user.Logins.Add(login);

                var userLogin = new IdentityUserLogin
                {
                    Id          = Util.GetLoginId(login),
                    UserId      = user.Id,
                    Provider    = login.LoginProvider,
                    ProviderKey = login.ProviderKey
                };
                await session.StoreAsync(userLogin);
            }
        }
コード例 #4
0
        public async Task Can_create_user_and_log_in()
        {
            const string username    = "******";
            const string userId      = "user_id_1";
            string       password    = Guid.NewGuid().ToString("n");
            const string googleLogin = "******";
            const string yahooLogin  = "******";

            var user = new SimpleAppUser {
                Id = userId, UserName = username
            };

            using (var docStore = NewDocStore())
            {
                using (var session = docStore.OpenAsyncSession())
                {
                    using (var mgr = new UserManager <SimpleAppUser>(new UserStore <SimpleAppUser>(session)))
                    {
                        IdentityResult result = mgr.Create(user, password);

                        Assert.True(result.Succeeded);
                        Assert.NotNull(user.Id);

                        var res1 = mgr.AddLogin(user.Id, new UserLoginInfo("Google", googleLogin));
                        var res2 = mgr.AddLogin(user.Id, new UserLoginInfo("Yahoo", yahooLogin));

                        Assert.True(res1.Succeeded);
                        Assert.True(res2.Succeeded);
                    }
                    await session.SaveChangesAsync();
                }

                using (var session = docStore.OpenSession())
                {
                    var loaded = session.Load <SimpleAppUser>(user.Id);
                    Assert.NotNull(loaded);
                    Assert.NotSame(loaded, user);
                    Assert.Equal(loaded.Id, user.Id);
                    Assert.Equal(loaded.UserName, user.UserName);
                    Assert.NotNull(loaded.PasswordHash);

                    Assert.Equal(loaded.Logins.Count, 2);
                    Assert.True(loaded.Logins.Any(x => x.LoginProvider == "Google" && x.ProviderKey == googleLogin));
                    Assert.True(loaded.Logins.Any(x => x.LoginProvider == "Yahoo" && x.ProviderKey == yahooLogin));

                    var loadedLogins = session.Advanced.LoadStartingWith <IdentityUserLogin>("IdentityUserLogins/");
                    Assert.Equal(loadedLogins.Length, 2);

                    foreach (var login in loaded.Logins)
                    {
                        var loginDoc = session.Load <IdentityUserLogin>(Util.GetLoginId(login));
                        Assert.Equal(login.LoginProvider, loginDoc.Provider);
                        Assert.Equal(login.ProviderKey, loginDoc.ProviderKey);
                        Assert.Equal(user.Id, loginDoc.UserId);
                    }
                }

                using (var session = docStore.OpenAsyncSession())
                {
                    using (var mgr = new UserManager <SimpleAppUser>(new UserStore <SimpleAppUser>(session)))
                    {
                        var userByName   = mgr.Find(username, password);
                        var userByGoogle = mgr.Find(new UserLoginInfo("Google", googleLogin));
                        var userByYahoo  = mgr.Find(new UserLoginInfo("Yahoo", yahooLogin));

                        Assert.NotNull(userByName);
                        Assert.NotNull(userByGoogle);
                        Assert.NotNull(userByYahoo);

                        Assert.Equal(userByName.Id, userId);
                        Assert.Equal(userByName.UserName, username);

                        // The Session cache should return the very same objects
                        Assert.Same(userByName, userByGoogle);
                        Assert.Same(userByName, userByYahoo);
                    }
                    await session.SaveChangesAsync();
                }
            }
        }