public async Task <IActionResult> CreateExp(RegisterViewModelAdmin model, int?CityId)
        {
            Random rand = new Random();
            string s    = model.UserName.Replace(" ", "") + rand.Next(1111, 9999);
            User   user = new User {
                PhoneNumber = model.PhoneNumber, UserName = s, Avatar = "Admin.png", Fio = model.UserName, PodCityId = CityId
            };

            if (user != null)
            {
                IdentityRole Role = new IdentityRole();
                Role = await _roleManager.Roles.FirstOrDefaultAsync(r => r.Name == "Исполнитель");

                if (Role != null)
                {
                    IdentityUserRole <string> userRole = new IdentityUserRole <string> {
                        UserId = user.Id, RoleId = Role.Id
                    };

                    await db.UserRoles.AddAsync(userRole);

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

                    if (result.Succeeded)
                    {
                        return(RedirectToAction("Users"));
                    }
                    else
                    {
                        foreach (var error in result.Errors)
                        {
                            ModelState.AddModelError(string.Empty, error.Description);
                        }
                    }
                }
                else
                {
                    ViewBag.Info = "Тип пользователя не наиден, повторите попытку!";
                }
            }
            else
            {
                ViewBag.Info = "User не наиден, повторите попытку занова!";
            }

            return(RedirectToAction("CreateUser"));
        }
Esempio n. 2
0
        public async Task <ActionResult> RegisterAdmin(RegisterViewModelAdmin model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.UserName, Email = model.Email, EmailConfirmed = true
                };
                //var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    //da ne se najavi odma
                    //await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    //string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    //await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    await this.UserManager.AddToRoleAsync(user.Id, model.UserRoles);

                    //return View("DisplayEmail");
                    //return RedirectToAction("Index", "Home");
                    return(RedirectToAction("createRating", "Ratings", new { userId = user.Id, type = "admin" }));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form

            //pri error
            ViewBag.Name = new SelectList(context.Roles.ToList(), "Name", "Name");
            return(View(model));
        }