示例#1
0
        public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email, FullName = model.FullName
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
                    // Send an email with this link
                    //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                    //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
                    //await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
                    //    $"Please confirm your account by clicking this link: <a href='{callbackUrl}'>link</a>");
                    bool adminExist = await _roleManager.RoleExistsAsync("SuperAdmin");

                    if (user.Email == "*****@*****.**" && !adminExist)
                    {
                        MyIdentityRole role = new MyIdentityRole();
                        role.Name        = "SuperAdmin";
                        role.Description = "Perform normal operations.";
                        IdentityResult roleResult = _roleManager.CreateAsync(role).Result;
                        if (!roleResult.Succeeded)
                        {
                            ModelState.AddModelError("",
                                                     "Error while creating role!");
                            return(View(model));
                        }
                        _userManager.AddToRoleAsync(user, "SuperAdmin").Wait();
                    }
                    //if (model.Email != "*****@*****.**")
                    //{
                    //    await _signInManager.SignInAsync(user, isPersistent: false);
                    //}
                    _logger.LogInformation(3, "User created a new account with password.");
                    return(RedirectToLocal(returnUrl));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
示例#2
0
        public async Task SeedDB()
        {
            var admin = await _userManager.FindByEmailAsync("*****@*****.**");

            if (admin == null)
            {
                var user = new ApplicationUser {
                    UserName = "******", Email = "*****@*****.**", FullName = "Admin Admin"
                };
                var result = await _userManager.CreateAsync(user, "admin123");

                MyIdentityRole role = new MyIdentityRole();
                role.Name        = "SuperAdmin";
                role.Description = "Perform normal operations.";
                IdentityResult roleResult = _roleManager.CreateAsync(role).Result;
                if (!roleResult.Succeeded)
                {
                    ModelState.AddModelError("",
                                             "Error while creating role!");
                }
                _userManager.AddToRoleAsync(user, "SuperAdmin").Wait();
            }
        }