Exemplo n.º 1
0
        public async Task <IHttpActionResult> Post(int id, AccountViewModel accountView)
        {
            //UserRoleRole userRoleRole = new UserRoleRole();
            if (id == 0)
            {
                /*
                 * t_User user=new t_User();
                 * user.LockoutEnabled = accountView.LockoutEnabled;
                 * user.Position = accountView.Position;
                 * user.SetPasswordEnabled = accountView.SetPasswordEnabled;
                 * user.FIO = accountView.FIO;
                 * user.UserName = accountView.UserName;
                 * _db.t_User.Add(user);
                 * _db.SaveChanges();
                 * _db.Entry(user).GetDatabaseValues();
                 * //int userId= _userManager.GetIdByName(_userName);
                 * await _userManager.AddUserToRoleAsync(user.UserID, accountView.UserRole);
                 * //userRoleRole.AddUserToRole(user.UserID, accountView.UserRole);*/
                //_userManager.UpdateAsync(user);
                if (await _userManager.CreateUserAsync(accountView) == IdentityResult.Success)
                {
                    return(Ok());
                }
                return(BadRequest());
            }
            else
            {
                //t_User user = _db.t_User.Find(id);
                var user = await _userManager.FindByIdAsync(id);

                if (user != null)
                {
                    user.LockoutEnabled     = accountView.LockoutEnabled;
                    user.Position           = accountView.Position;
                    user.SetPasswordEnabled = accountView.SetPasswordEnabled;
                    user.FIO      = accountView.FIO;
                    user.UserName = accountView.UserName;
                    //_db.Entry(user).State=EntityState.Modified;
                    //_db.SaveChanges();
                    await _userManager.UpdateAsync(user);

                    //int userId = _userManager.GetIdByName(_userName);
                    await _userManager.RemoveUserFromRoleAsync(id);

                    //userRoleRole.RemoveUserFromRole(id, accountView.UserRole);

                    await _userManager.AddUserToRoleAsync(id, accountView.UserRole);

                    //userRoleRole.AddUserToRole(id, accountView.UserRole);
                    //_userManager.UpdateAsync(user);
                    return(Ok());
                }
                else
                {
                    return(BadRequest());
                }
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Register([FromBody] RegisterPostBindingModel model, string id, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            AppUser appUser = new AppUser(model.Email);

            appUser.Devices.Add(Guid.Parse(id));

            var result = await _userManager.CreateUserAsync(appUser, model.Password);

            if (result)
            {
                return(Ok($"{model.Email} has been register succesfully"));
            }
            return(BadRequest("something has gone wrong check if email is correct"));
        }