Esempio n. 1
0
        public override async Task <IList <string> > GetRolesAsync(MangoUser user, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            UserEntity userEntity = await FindByNameEntityAsync(user.UserName);

            IList <string> roles        = new List <string>();
            var            roleentities = await _userDbContext.User2Roles.Where(u2r => u2r.UserId == userEntity.Id).Select(u2r => u2r.RoleId).ToListAsync();

            foreach (int i in roleentities)
            {
                var role = await _userDbContext.MangoUserRoles.Where(r => r.Id == i).SingleOrDefaultAsync();

                if (role != null)
                {
                    roles.Add(role.RoleName);
                }
            }
            return(roles);
        }
Esempio n. 2
0
        public override async Task AddToRoleAsync(MangoUser user, string roleName, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            var role = await _userDbContext.MangoUserRoles.Where(r => r.RoleName == roleName).SingleOrDefaultAsync();

            if (role == null)
            {
                throw new RoleNotFoundException($"没有找到角色为:{roleName}的对象");
            }
            var mangouser = await FindByNameEntityAsync(user.UserName);

            User2Role u2r = new User2Role
            {
                UserId = mangouser.Id,
                RoleId = role.Id
            };
            await _userDbContext.User2Roles.AddAsync(u2r);
        }
Esempio n. 3
0
        public override async Task <IList <UserLoginInfo> > GetLoginsAsync(MangoUser user, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            IList <UserLoginInfo> userLoginInfos;
            UserEntity            userEntity = await FindByNameEntityAsync(user.UserName);

            if (userEntity == null)
            {
                return(null);
            }
            var logins = await _userDbContext.ExternalLogins.Where(ex => ex.UserId == userEntity.Id).ToListAsync();

            userLoginInfos = new List <UserLoginInfo>();
            foreach (ExternalLoginEntity externalLogin in logins)
            {
                userLoginInfos.Add(MEConversion.ExternalLoginE2M(externalLogin));
            }

            return(userLoginInfos);
        }
Esempio n. 4
0
        public override async Task RemoveLoginAsync(MangoUser user, string loginProvider, string providerKey, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (loginProvider == null)
            {
                throw new ArgumentNullException(nameof(loginProvider));
            }
            if (providerKey == null)
            {
                throw new ArgumentNullException(nameof(providerKey));
            }

            UserEntity userEntity = await FindByNameEntityAsync(user.UserName);

            if (userEntity == null)
            {
                return;
            }
            var logins = await _userDbContext.ExternalLogins.Where(ex => ex.UserId == userEntity.Id && ex.LoginProvider == loginProvider && ex.ProviderKey == providerKey).ToListAsync();

            _userDbContext.RemoveRange(logins);
        }
Esempio n. 5
0
        public override async Task RemoveFromRoleAsync(MangoUser user, string roleName, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            if (roleName == null)
            {
                throw new ArgumentNullException(nameof(roleName));
            }

            var userentity = await FindByNameEntityAsync(user.UserName);

            if (userentity == null)
            {
                throw new UserNotFoundException(user.UserName);
            }
            IList <User2Role> user2Roles = await _userDbContext.User2Roles.Where(u2r => u2r.UserId == userentity.Id).ToListAsync();

            _userDbContext.User2Roles.RemoveRange(user2Roles);
        }
Esempio n. 6
0
        public override async Task <IdentityResult> CreateAsync(MangoUser user, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            try
            {
                await _userDbContext.MangoUsers.AddAsync(MEConversion.UserM2E(user));

                await _userDbContext.SaveChangesAsync();

                _userDbContext.ChangeTracker.Entries <UserEntity>().First().State = EntityState.Detached;
            }
            catch (DbUpdateConcurrencyException e)
            {
                return(IdentityResult.Failed(new IdentityError {
                    Description = e.Message
                }));
            }
            return(IdentityResult.Success);
        }
Esempio n. 7
0
        public override async Task <IdentityResult> DeleteAsync(MangoUser user, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            try
            {
                var mangouser = await FindByNameEntityAsync(user.LoginName);

                if (mangouser == null)
                {
                    throw new UserNotFoundException(user.UserName);
                }
                _userDbContext.Remove(mangouser);
                await _userDbContext.SaveChangesAsync();
            }
            catch (DbUpdateException e)
            {
                return(IdentityResult.Failed(new IdentityError {
                    Description = e.Message
                }));
            }
            return(IdentityResult.Success);
        }
Esempio n. 8
0
        public override async Task ReplaceClaimAsync(MangoUser user, Claim claim, Claim newClaim, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (claim == null)
            {
                throw new ArgumentNullException(nameof(claim));
            }
            if (newClaim == null)
            {
                throw new ArgumentNullException(nameof(newClaim));
            }

            UserEntity userEntity = await FindByNameEntityAsync(user.UserName);

            if (userEntity == null)
            {
                throw new UserNotFoundException(user.UserName);
            }
            int             userid   = userEntity.Id;
            UserClaimEntity oldclaim = await _userDbContext.MangoUserClaims.Where(c => c.UserId == userid && c.ClaimType == claim.Type && c.ClaimValue == claim.Value).SingleOrDefaultAsync();

            if (oldclaim == null)
            {
                throw new ClaimNotFoundException(claim.Type, claim.Value);
            }
            oldclaim.ClaimType  = newClaim.Type;
            oldclaim.ClaimValue = newClaim.Value;
        }
Esempio n. 9
0
        static UserData()
        {
            TestUser1 = new MangoUser
            {
                Id              = 3,
                UserName        = "******",
                Email           = "*****@*****.**",
                NormalizedEmail = "*****@*****.**",
                LoginName       = "TEST1",
            };

            TestUser2 = new MangoUser
            {
                Id              = 2,
                UserName        = "******",
                Email           = "*****@*****.**",
                NormalizedEmail = "*****@*****.**",
                LoginName       = "TEST2",
            };

            Mango = new MangoUser
            {
                Id              = 4,
                UserName        = "******",
                LoginName       = "MANGO",
                Email           = "*****@*****.**",
                NormalizedEmail = "*****@*****.**",
                Claims          = null,
                Password        = "******",
                EmailConfirmed  = false,
                CreateDate      = new DateTime(2020, 03, 07),
                LastLoginDate   = new DateTime(2020, 04, 07)
            };
        }
Esempio n. 10
0
        public override async Task AddClaimsAsync(MangoUser user, IEnumerable <Claim> claims, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (claims == null)
            {
                throw new ArgumentNullException(nameof(claims));
            }
            MangoUser mangoUser = await FindByNameAsync(user.UserName, cancellationToken);

            int userid = mangoUser.Id;

            foreach (Claim claim in claims)
            {
                UserClaimEntity claimEntity = new UserClaimEntity
                {
                    UserId     = userid,
                    ClaimType  = claim.Type,
                    ClaimValue = claim.Value
                };
                await _userDbContext.AddAsync(claimEntity);
            }
        }
Esempio n. 11
0
        public void FindByName(string userName, MangoUser except)
        {
            UserManagerAction(usermanager =>
            {
                var user = usermanager.FindByNameAsync(userName).Result;

                AssertUserEqual(except, user);
            });
        }
Esempio n. 12
0
        public override async Task <string> GetUserNameAsync(MangoUser user, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            return(await Task.FromResult(user.UserName));
        }
Esempio n. 13
0
        public async Task <IActionResult> SignUp([Bind("UserName,Email,Password,PasswordConfirm,IsAgree")] SignUpInputModel signUpInputModel)
        {
            if (signUpInputModel == null)
            {
                throw new ArgumentNullException(nameof(signUpInputModel));
            }

            if (!ModelState.IsValid)
            {
                return(View(signUpInputModel));
            }

            MangoUser mangoUser = new MangoUser
            {
                UserName = signUpInputModel.UserName,
                Email    = signUpInputModel.Email
            };

            if (await _userManager.FindByNameAsync(mangoUser.UserName) != null)
            {
                ModelState.AddModelError("UserName", "该用户名已被注册");
                return(View(signUpInputModel));
            }

            using (var trans = await _transaction.BeginTransactionAsync())
            {
                try
                {
                    var flag = await _userManager.CreateAsync(mangoUser, signUpInputModel.Password);

                    mangoUser = await _userManager.FindByNameAsync(mangoUser.UserName);

                    var roleFlag = await _userManager.AddToRoleAsync(mangoUser, "USER");

                    if (!flag.Succeeded || !roleFlag.Succeeded)
                    {
                        ViewData["Message"] = "Registration error occurred!";
                        trans.Rollback();
                    }
                    else
                    {
                        ViewData["Message"] = "Registration Successful!";
                        trans.Commit();
                    }
                }
                catch
                {
                    trans.Rollback();
                    throw;
                }
            }

            return(View("ResultPage"));
        }
Esempio n. 14
0
 /// <summary>
 /// 判断用户相等的辅助方法
 /// </summary>
 /// <param name="excepted"></param>
 /// <param name="actual"></param>
 protected static void AssertUserEqual(MangoUser excepted, MangoUser actual)
 {
     Assert.Equal(excepted.UserName, actual.UserName);
     Assert.Equal(excepted.LoginName, actual.LoginName);
     Assert.Equal(excepted.Email, actual.Email);
     Assert.Equal(excepted.NormalizedEmail, actual.NormalizedEmail);
     Assert.Equal(excepted.EmailConfirmed, actual.EmailConfirmed);
     Assert.Equal(excepted.Password, actual.Password);
     Assert.Equal(excepted.LastLoginDate, actual.LastLoginDate);
     Assert.Equal(excepted.CreateDate, actual.CreateDate);
 }
Esempio n. 15
0
        public override async Task <bool> HasPasswordAsync(MangoUser user, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            UserEntity userEntity = await FindByNameEntityAsync(user.UserName);

            return((userEntity.Password != null) ? true : false);
        }
Esempio n. 16
0
        public override async Task SetEmailConfirmedAsync(MangoUser user, bool confirmed, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            await Task.Run(() =>
            {
                user.EmailConfirmed = confirmed;
            });
        }
Esempio n. 17
0
        public async Task <IActionResult> User()
        {
            bool isauth = base.User.Identity.IsAuthenticated;

            if (!isauth)
            {
                return(Redirect("/"));
            }
            string username = base.User.Identity.Name;

            MangoUser mangoUser = await _userManager.FindByNameAsync(username);

            return(View(mangoUser));
        }
Esempio n. 18
0
        public override async Task <string> GetLoginNameAsync(MangoUser user, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (user.LoginName == null)
            {
                return(user.LoginName);
            }
            var mangouser = await FindByIdAsync(user.Id, cancellationToken);

            return(mangouser.LoginName);
        }
Esempio n. 19
0
        public override async Task <string> GetPasswordHashAsync(MangoUser user, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            UserEntity userEntity = await FindByNameEntityAsync(user.UserName);

            if (userEntity == null)
            {
                return(null);
            }
            return(userEntity.Password);
        }
Esempio n. 20
0
        public override async Task <bool> GetEmailConfirmedAsync(MangoUser user, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            UserEntity userEntity = await FindByNameEntityAsync(user.UserName);

            if (userEntity == null)
            {
                return(false);
            }
            return(userEntity.EmailConfirmed);
        }
Esempio n. 21
0
        public override async Task SetNormalizedEmailAsync(MangoUser user, string normalizedEmail, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (normalizedEmail == null)
            {
                throw new ArgumentNullException(nameof(normalizedEmail));
            }
            await Task.Run(() =>
            {
                user.NormalizedEmail = normalizedEmail;
            });
        }
Esempio n. 22
0
        public void CreateUserTest(string username, string email, MangoUser except, bool result)
        {
            MangoUser mangoUser = new MangoUser
            {
                UserName = username,
                Email    = email
            };

            UserManagerAction(usermanager =>
            {
                var flag = usermanager.CreateAsync(mangoUser).Result;

                var user = usermanager.FindByNameAsync(username).Result;

                Assert.Equal(result, flag.Succeeded);
                AssertUserEqual(except, user);
            });
        }
Esempio n. 23
0
        public override async Task SetUserNameAsync(MangoUser user, string userName, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (userName == null)
            {
                throw new ArgumentNullException(nameof(userName));
            }

            await Task.Run(() =>
            {
                user.UserName = userName;
            });
        }
Esempio n. 24
0
        public override async Task SetPasswordHashAsync(MangoUser user, string passwordHash, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (passwordHash == null)
            {
                throw new ArgumentNullException(nameof(passwordHash));
            }

            await Task.Run(() =>
            {
                user.Password = passwordHash;
            });
        }
Esempio n. 25
0
 public static UserEntity UserM2E(MangoUser mangoUser)
 {
     if (mangoUser == null)
     {
         throw new ArgumentNullException(nameof(mangoUser));
     }
     return(new UserEntity
     {
         Id = mangoUser.Id,
         LoginName = mangoUser.LoginName,
         UserName = mangoUser.UserName,
         Password = mangoUser.Password,
         Email = mangoUser.Email,
         NormalizedEmail = mangoUser.NormalizedEmail,
         EmailConfirmed = mangoUser.EmailConfirmed,
         CreateDate = mangoUser.CreateDate,
         LastLoginDate = mangoUser.LastLoginDate
     });
 }
Esempio n. 26
0
        public override async Task <IList <Claim> > GetClaimsAsync(MangoUser user, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            UserEntity userEntity = await FindByNameEntityAsync(user.LoginName);

            IList <UserClaimEntity> userClaimEntities = await _userDbContext.MangoUserClaims.Where(c => c.UserId == userEntity.Id).ToListAsync();

            IList <Claim> mangoUserClaims = new List <Claim>();

            foreach (UserClaimEntity claimEntity in userClaimEntities)
            {
                Claim claim = new Claim(claimEntity.ClaimType, claimEntity.ClaimValue);
                mangoUserClaims.Add(claim);
            }
            return(mangoUserClaims);
        }
Esempio n. 27
0
        public override async Task <bool> IsInRoleAsync(MangoUser user, string roleName, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null || roleName == null)
            {
                return(false);
            }

            var userentity = await FindByNameEntityAsync(user.UserName);

            if (userentity == null)
            {
                return(false);
            }

            int userid = userentity.Id;
            var flag   = await _userDbContext.User2Roles.Where(u2r => u2r.UserId == userid).AnyAsync();

            return(flag);
        }
Esempio n. 28
0
        public override async Task RemoveClaimsAsync(MangoUser user, IEnumerable <Claim> claims, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (claims == null)
            {
                throw new ArgumentNullException(nameof(claims));
            }
            UserEntity userEntity = await FindByNameEntityAsync(user.UserName);

            if (userEntity == null)
            {
                throw new UserNotFoundException(user.UserName);
            }
            int userid = userEntity.Id;
            IList <UserClaimEntity> userClaimEntities = await _userDbContext.MangoUserClaims.Where(c => c.UserId == userid).ToListAsync();

            IList <UserClaimEntity> removeList = new List <UserClaimEntity>();

            foreach (Claim claim in claims)
            {
                foreach (UserClaimEntity userClaim in userClaimEntities)
                {
                    if (claim.Type == userClaim.ClaimType && claim.Value == userClaim.ClaimValue)
                    {
                        removeList.Add(userClaim);
                    }
                }
            }

            _userDbContext.MangoUserClaims.RemoveRange(removeList);
        }
Esempio n. 29
0
        public override async Task <IdentityResult> UpdateAsync(MangoUser user, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            _userDbContext.Update(MEConversion.UserM2E(user));

            try
            {
                await _userDbContext.SaveChangesAsync();
            }
            catch (DbUpdateException e)
            {
                return(IdentityResult.Failed(new IdentityError {
                    Description = e.Message
                }));
            }
            return(IdentityResult.Success);
        }
Esempio n. 30
0
        public override async Task AddLoginAsync(MangoUser user, UserLoginInfo login, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            ThrowIfDisposed();

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            if (login == null)
            {
                throw new ArgumentNullException(nameof(login));
            }

            var mangouser = await FindByNameEntityAsync(user.UserName);

            if (mangouser != null)
            {
                ExternalLoginEntity externalLogin = MEConversion.ExternalLoginM2E(login);
                externalLogin.UserId = mangouser.Id;

                await _userDbContext.ExternalLogins.AddAsync(externalLogin);
            }
        }