コード例 #1
0
        public UserModel AddUser(UserModel userModel)
        {
            if (userModel.Image != null)
            {
                string extension = Path.GetExtension(userModel.Image.FileName);

                userModel.ImageFileName = Guid.NewGuid() + extension;

                using (FileStream fileStream = File.Create("Uploads/" + userModel.ImageFileName))
                {
                    userModel.Image.CopyTo(fileStream);
                }
                userModel.Image = null;
            }
            using (DB.Database.BeginTransaction())
            {
                if (userModel.Role == null)
                {
                    userModel.Role = "user";
                }

                byte[] salt = new byte[128 / 8];
                using (var rng = RandomNumberGenerator.Create())
                {
                    rng.GetBytes(salt);
                }
                userModel.Salt     = Convert.ToBase64String(salt);
                userModel.Password = Convert.ToBase64String(KeyDerivation.Pbkdf2(
                                                                password: userModel.Password,
                                                                salt: salt,
                                                                prf: KeyDerivationPrf.HMACSHA1,
                                                                iterationCount: 10000,
                                                                numBytesRequested: 256 / 8));

                User addedUser = userModel.ConvertToUser();
                DB.Users.Add(addedUser);
                DB.SaveChanges();

                if (userModel.City != null && userModel.AdressLine != null)
                {
                    Adress addedAdress = userModel.ConvertToAdress();
                    DB.Adresses.Add(addedAdress);
                    DB.SaveChanges();

                    UsersAdress usersAdress = new UsersAdress
                    {
                        UserId   = addedUser.UserId,
                        AdressId = addedAdress.AdressId
                    };
                    DB.SaveChanges();
                }

                BranchUser branchUserToAdd = new BranchUser
                {
                    UserId   = addedUser.UserId,
                    BranchId = userModel.BranchId
                };
                DB.BranchUsers.Add(branchUserToAdd);
                DB.SaveChanges();

                DB.Database.CommitTransaction();

                userModel.UserId = addedUser.UserId;
            }
            return(userModel);
        }
コード例 #2
0
 public UsersAdressModel(UsersAdress usersAdress)
 {
     UserAdressId = usersAdress.UserAdressId;
     UserId       = usersAdress.UserId;
     AdressId     = usersAdress.AdressId;
 }