Beispiel #1
0
        public async Task <JsonResult> Register(UserAddVM model)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    User entity = _mapper.Map <UserAddVM, User>(model);
                    entity.Id                = Guid.NewGuid();
                    entity.CreateDateTime    = DateTime.UtcNow;
                    entity.LastLoginDateTime = DateTime.UtcNow;
                    var identityResult = await _userManager.CreateAsync(entity, model.PasswordHash);

                    if (identityResult.Succeeded)
                    {
                        await _signInManager.SignInAsync(entity, isPersistent : false);


                        UserAuthenticateVM returnVM = new UserAuthenticateVM();
                        returnVM       = _mapper.Map <User, UserAuthenticateVM>(entity);
                        returnVM.Token = GetToken(entity);

                        return(new JsonResult(returnVM));
                    }
                    else
                    {
                        return(new JsonResult(APIResult.CreateVMWithRec <object>(identityResult.Errors)));
                    }
                }
            }
            catch (Exception ex)
            {
                return(new JsonResult(APIResult.CreateVMWithError(ex, APIResult.CreateVM(false, null, AppStatusCode.ERR01001))));
            }
            return(new JsonResult(APIResult.CreateVM(false, null, AppStatusCode.ERR01001)));
        }
Beispiel #2
0
        public async Task <ActionResult> AddUser(UserAddVM model)
        {
            if (ModelState.IsValid)
            {
                var context = new ApplicationDbContext();
                var userMgr = new UserManager <AppUser>(new UserStore <AppUser>(context));
                var roleMgr = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));

                try
                {
                    var user = new AppUser()
                    {
                        FirstName = model.User.FirstName,
                        LastName  = model.User.LastName,
                        Email     = model.User.Email,
                        UserName  = model.User.Email
                    };

                    var result = await userMgr.CreateAsync(user, model.Password);

                    var role = roleMgr.FindById(model.SelectedRoleID.ToString());

                    if (result.Succeeded)
                    {
                        result = userMgr.AddToRole(user.Id, role.Name);
                    }
                    return(RedirectToAction("Users", new { id = user.Id }));
                }

                catch (Exception ex)
                {
                    throw ex;
                }
            }
            else
            {
                var context = new ApplicationDbContext();

                var roleMgr = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));
                var list    = roleMgr.Roles.ToList();

                model.RoleList = list.Select(x => new SelectListItem()
                {
                    Text  = x.Name,
                    Value = x.Id
                });

                return(View(model));
            }
        }
Beispiel #3
0
        public IActionResult Add(string returnUrl = null)
        {
            var model = new UserAddVM
            {
                UserClaims = ClaimData.UserClaims.Select(c => new SelectListItem
                {
                    Text  = c,
                    Value = c
                }).ToList()
            };

            ViewData["ReturnUrl"] = returnUrl;
            return(View(model));
        }
Beispiel #4
0
        public ActionResult AddUser()
        {
            var model   = new UserAddVM();
            var context = new ApplicationDbContext();
            var roleMgr = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));
            var list    = roleMgr.Roles.ToList();

            model.RoleList = list.Select(x => new SelectListItem()
            {
                Text  = x.Name,
                Value = x.Id
            });

            return(View(model));
        }
Beispiel #5
0
        public async Task <IActionResult> Add(UserAddVM model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                var email = new MailAddress(model.Email);

                var user = new ApplicationUser
                {
                    UserName  = email.User,
                    Email     = email.Address,
                    FirstName = model.FirstName,
                    LastName  = model.LastName
                };

                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    // Add User Claims for full name. You can check for the success of addition
                    await _userManager.AddClaimAsync(user, new Claim("FirstName", user.FirstName));

                    await _userManager.AddClaimAsync(user, new Claim("LastName", user.LastName));

                    var userClaims = model.UserClaims.Where(c => c.Selected).ToList();
                    foreach (var claim in userClaims)
                    {
                        await _userManager.AddClaimAsync(user, new Claim(claim.Value, claim.Value));
                    }

                    return(RedirectToLocal(returnUrl));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Beispiel #6
0
 public IActionResult AddUser(UserAddVM model)
 {
     _bILoginRepository.Add(model);
     return(Ok(true));
 }