コード例 #1
0
 //SendEmailConfirmation for selected User.
 public async Task SendEmailConfirmation(ApplicationDbUser user, string callbackUrl)
 {
     await _emailSender.SendEmailAsync(
         user.Email,
         "Confirm your email",
         $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>."
         );
 }
コード例 #2
0
        //GenerateEmailConfirmation for selected User.
        public async Task <string> GenerateEmailConfirmation(ApplicationDbUser user)
        {
            var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

            var callbackUrl = Url.Page(
                "/Account/ConfirmEmail",
                pageHandler: null,
                values: new { userId = user.Id, code = code },
                protocol: Request.Scheme
                );

            return(callbackUrl);
        }
コード例 #3
0
        //Use method to make first User Admin.
        public async Task FirstAccountAdmin(ApplicationDbUser user)
        {
            //Check role(Admin) is created
            if (!await _roleManager.RoleExistsAsync("Admin"))
            {
                //Create the new role: Admin
                var roleAdmin = new IdentityRole();
                roleAdmin.Name = "Admin";
                await _roleManager.CreateAsync(roleAdmin);

                //Create the new role: Manager
                var roleManager = new IdentityRole();
                roleManager.Name = "Manager";
                await _roleManager.CreateAsync(roleManager);

                //Create the new role: Support
                var roleSupport = new IdentityRole();
                roleSupport.Name = "Support";
                await _roleManager.CreateAsync(roleSupport);

                //Create the new role: Customer
                var roleCustomer = new IdentityRole();
                roleCustomer.Name = "Customer";
                await _roleManager.CreateAsync(roleCustomer);

                //Assign role to seleced user.
                await _userManager.AddToRoleAsync(user, roleAdmin.Name);

                //Sign User in.
                await _signInManager.SignInAsync(user, isPersistent : false);

                //Send emailconfirmation to new created user.
                var callbaclUrl = await GenerateEmailConfirmation(user);
                await SendEmailConfirmation(user, callbaclUrl);
            }
        }
コード例 #4
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            //Used to set redirect-option. This will redirect User back to the page which they come from,
            //after some process is completed.
            returnUrl = returnUrl ?? Url.Content("~/");

            if (ModelState.IsValid)
            {
                //Create new User
                var user = new ApplicationDbUser {
                    UserName = Input.Email, Email = Input.Email
                };
                var userCreatedResult = await _userManager.CreateAsync(user, Input.Password);

                if (userCreatedResult.Succeeded)
                {
                    //Create first user as Admin.
                    if (_userManager.Users.ToList().Count == 1)
                    {
                        await FirstAccountAdmin(user);

                        //Using LocalRedirect to ensures that the "returnUrl" is a route actually on your site. For safe.
                        return(LocalRedirect("/Index"));
                    }
                    else
                    {
                        //Checks if role already exists.
                        var resultAlreadyCreatedRole = await _roleManager.RoleExistsAsync("Customer");

                        //If role DOES NOT already exists.
                        if (resultAlreadyCreatedRole == false)
                        {
                            //Create the new role.
                            var role = new IdentityRole();
                            role.Name = "Customer";
                            await _roleManager.CreateAsync(role);

                            //Assign role to seleced user.
                            await _userManager.AddToRoleAsync(user, role.Name);

                            StatusMessage = $"User created with Email: {Input.Email}. Please check your Email to confirm it.";
                        }
                        else
                        {
                            //Assign role to seleced user.
                            await _userManager.AddToRoleAsync(user, "Customer");

                            StatusMessage = $"User created with Email: {Input.Email}. Please check your Email to confirm it.";
                        }

                        //Sign created User in.
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        //Send emailconfirmation to new created user.
                        var callbaclUrl = await GenerateEmailConfirmation(user);
                        await SendEmailConfirmation(user, callbaclUrl);

                        //Using LocalRedirect to ensures that the "returnUrl" is a route actually on your site. For safe.
                        return(LocalRedirect("/Index"));
                    }
                }
                //Handle errors
                foreach (var error in userCreatedResult.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            //if we reach this, something failed! Just stay on current page.
            return(Page());
        }