コード例 #1
0
ファイル: UserController.cs プロジェクト: Nodios/RepacMono
        public async Task<ActionResult> Create(RegisterViewModel model, ApplicationUserModel user)
        {
            if (ModelState.IsValid)
            {
                //http://prntscr.com/9q077y
                user = new ApplicationUserModel { Id = user.Id, UserName=model.UserName, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, OIB = model.OIB, RedCrossId = model.RedCrossId };

                //Register new user: http://prntscr.com/9q0bo6
                await Service.RegisterUser(AutoMapper.Mapper.Map<IApplicationUser>(user), model.Password);

                return RedirectToAction("Index");

            }
            else
            {
                ViewBag.RedCross = await RedService.GetAllAsync(null);
                return View();
            }
        }
コード例 #2
0
ファイル: UserController.cs プロジェクト: Nodios/RepacMono
        public async Task<ActionResult> Edit(ApplicationUserModel editUser, string userId, string roleId)
        {
            if (userId == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest);

            var user = await UserManager.FindByIdAsync(userId);
            user.UserName = editUser.UserName;
            if (ModelState.IsValid)
            {
                await UserManager.UpdateAsync(user);

                var rolesForUser = await UserManager.GetRolesAsync(userId);
                if (rolesForUser.Count() > 0)
                {
                    foreach (var item in rolesForUser)
                    {
                        var result = await UserManager.RemoveFromRoleAsync(userId, item);
                    }
                }

                if (!String.IsNullOrWhiteSpace(roleId))
                {
                    var role = await RoleManager.FindByIdAsync(roleId);

                    var result = await UserManager.AddToRoleAsync(userId, role.Name);
                    if (!result.Succeeded)
                    {
                        ModelState.AddModelError("", result.Errors.First().ToString());
                        return View();
                    }
                }
                return RedirectToAction("Index");
            }
            else
            {
                return View();
            }
        }