Exemple #1
0
        public async Task <IdentityResult> CreateAsync(TUser user, CancellationToken cancellationToken)
        {
            try
            {
                if (user == null)
                {
                    throw new ArgumentNullException("user");
                }

                var foundUser = await userTable.GetUserByEmail(user.Email, cancellationToken);

                if (foundUser != null)
                {
                    return(IdentityResult.Success);
                }

                TUserKey insertResult = await userTable.Insert(user, cancellationToken);

                user.Id = insertResult;
                return(IdentityResult.Success);
            }
            catch (Exception ex)
            {
                return(IdentityResult.Failed(new IdentityError()
                {
                    Code = ex.HResult.ToString(), Description = ex.Message
                }));
            }
        }
        static void Main(string[] args)
        {
            User fengqingyang = new User()
            {
                ID   = "1",
                Name = "风清扬"
            };
            Department pc1depart = new Department()
            {
                ID        = 1,
                DeprtName = "PC软件一部",
            };

            //IFactory factory = new SQLserverFcatory();  //想用哪个数据库就用哪个数据库
            //IUserTable userTable = factory.CreateUserTable();
            //userTable.Insert(fengqingyang);
            //userTable.GetUser(1);

            //IFactory factory2 = new AccessFactory();
            //IUserTable userTable2 = factory2.CreateUserTable();
            //userTable2.Insert(fengqingyang);
            //userTable2.GetUser(1);

            #region 利用简单工厂加反射,实现不同平台数据库读取
            IUserTable userTable = DataAccess.CreateUserTable();
            userTable.Insert(fengqingyang);
            IDepartment department = DataAccess.CreateDeprtTable();
            department.Insert(pc1depart);

            #endregion

            Console.ReadKey();
        }
        public async Task <User> CreateUser(User user, string password)
        {
            if (!(await _userTable.SelectByUsername(user.Username)).Any())                    // Check if username already exists
            {
                string hashedPassword = BCrypt.HashPassword(password, BCrypt.GenerateSalt()); // Hash password

                await _userTable.Insert(user, hashedPassword);                                // Insert user into database

                user = await GetUserByUsername(user.Username);

                foreach (Role role in user.Roles)
                {
                    if ((await _roleTable.SelectById(role.Id)).Any())                                          // Check if role exists
                    {
                        await _userRoleTable.Insert(new UserRoleModel { UserId = user.Id, RoleId = role.Id }); // Insert new user role relation
                    }
                    else
                    {
                        throw new ArgumentException("Provided role does not exist.");
                        // TODO: Could fail gracefully and simply not add role
                    }
                }

                return(await GetUserById(user.Id));
            }
            else
            {
                throw new ArgumentException("Provided username is already in use.");
            }
        }