示例#1
0
        public Token Register(string username, string password)
        {
            var user = _userRepository.GetByLogin(username);

            if (user != null)
            {
                throw new AuthenticationException("Username already exists");
            }
            using (var uow = _unitOfWorkFactory.Create())
            {
                var systemRoles = _roleStore.GetSystemRoles <Models.Entities.Role>();

                user = new User()
                {
                    Login        = username,
                    PasswordHash = HashPassword(password)
                };
                if (_userRepository.CreateQuery().GetTotalRowCount() == 0)
                {
                    //Creating first user which must be admin
                    var adminRole = systemRoles.First(r => r.Type == RoleType.Admin);
                    user.UserRoles.Add(new UserRole(user, adminRole));
                }
                _userRepository.Insert(user);
                var token = CreateToken(user);

                uow.Commit();
                return(token);
            }
        }