Beispiel #1
0
        //[ValidateAntiForgeryToken]
        public async Task <ActionResult> Update(ApplicationUserViewModel aspNetUser)
        {
            //ViewBag.BranchList = _branchService.GetBranchList();

            BaseResponse         baseResponse = new BaseResponse();
            ApplicationDbContext context      = new ApplicationDbContext();
            var UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context));
            var user        = UserManager.FindById(aspNetUser.Id);

            user.PasswordHash = UserManager.PasswordHasher.HashPassword(aspNetUser.Password);
            user.Password     = DataProtectionLib.Encrypt(aspNetUser.Password);
            user.Active       = aspNetUser.Active;
            user.FirstName    = aspNetUser.FirstName;
            user.LastName     = aspNetUser.LastName;
            user.PhoneNo      = aspNetUser.PhoneNo;
            user.Email        = aspNetUser.Email;

            IdentityResult result = await UserManager.UpdateAsync(user);

            if (result.Succeeded)
            {
                baseResponse.Success = true;
            }
            else
            {
                baseResponse.Success = false;
                baseResponse.Message = "User not updated.!";
            }

            return(new JsonResult {
                Data = baseResponse, JsonRequestBehavior = JsonRequestBehavior.AllowGet
            });
        }
Beispiel #2
0
        //[ValidateAntiForgeryToken]
        public ActionResult GetUser(string UserId)
        {
            using (dbLawCMSEntities db = new dbLawCMSEntities())
            {
                //List<AspNetUser> v = db.AspNetUsers.ToList();

                var users = (
                    from a in db.AspNetUsers
                    //join b in db.AspNetRoles on a.Id equals b.Id
                    where a.Id == UserId
                    select new ApplicationUserViewModel()
                {
                    Id = a.Id,
                    Email = a.Email,
                    Password = a.Password,
                    Active = a.Active,
                    FirstName = a.FirstName,
                    LastName = a.LastName,
                    PhoneNo = a.PhoneNo,

                    //Branch = b.BranchName,
                    //Active = a.Active == true ? "Yes" : "No"
                }
                    ).ToList();


                foreach (var item in users)
                {
                    if (item.Password != null)
                    {
                        item.Password        = DataProtectionLib.Decrypt(item.Password);
                        item.ConfirmPassword = item.Password;
                    }
                }

                return(new JsonResult {
                    Data = users, JsonRequestBehavior = JsonRequestBehavior.AllowGet
                });
            }
        }
Beispiel #3
0
        //[ValidateAntiForgeryToken]
        public ActionResult UserCreate(ApplicationUserViewModel model)
        {
            //ViewBag.BranchList = _branchService.GetBranchList();
            BaseResponse baseResponse = new BaseResponse();
            // model.Role = "Employee";
            ApplicationDbContext context = new ApplicationDbContext();

            var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));
            var UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context));

            if (ModelState.IsValid)
            {
                var checkUser = UserManager.FindByNameAsync(model.Email);
                if (checkUser.Result == null)
                {
                    string newPassword = DataProtectionLib.Encrypt(model.Password);
                    var    user        = new ApplicationUser
                    {
                        UserName  = model.Email,
                        Email     = model.Email,
                        Password  = newPassword,
                        Active    = model.Active,
                        FirstName = model.FirstName,
                        LastName  = model.LastName,
                        PhoneNo   = model.PhoneNo,
                    };
                    var result = UserManager.Create(user, model.Password);
                    if (result.Succeeded)
                    {
                        var result1 = UserManager.AddToRole(user.Id, model.Role);
                        if (result1.Succeeded)
                        {
                            baseResponse.Success = true;
                        }
                        else
                        {
                            deleteApplicationUser(user.Id);
                            baseResponse.Success = false;
                            baseResponse.Message = "User not created";
                        }
                    }
                    else
                    {
                        baseResponse.Success = false;
                        baseResponse.Message = "User not created";
                    }
                }
                else
                {
                    baseResponse.Success = false;
                    baseResponse.Message = "User already exist";
                }
            }
            else
            {
                baseResponse.Success = false;
                baseResponse.Message = "Please enter valid data";
            }

            return(new JsonResult {
                Data = baseResponse, JsonRequestBehavior = JsonRequestBehavior.AllowGet
            });
        }