Example #1
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl      = returnUrl ?? Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                //var user = new IdentityUser { UserName = Input.Email, Email = Input.Email };
                //we prefer to use our class Application User
                var user = new ApplicationUser
                {
                    UserName      = Input.Email,
                    Email         = Input.Email,
                    CompanyId     = Input.CompanyId,
                    StreetAddress = Input.StreetAddress,
                    City          = Input.City,
                    State         = Input.State,
                    PostalCode    = Input.PostalCode,
                    Name          = Input.Name,
                    PhoneNumber   = Input.PhoneNumber,
                    Role          = Input.Role
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");
                    if (!await _roleManager.RoleExistsAsync(SD.Role_Admin))
                    {
                        await _roleManager.CreateAsync(new IdentityRole(SD.Role_Admin));
                    }
                    if (!await _roleManager.RoleExistsAsync(SD.Role_Employee))
                    {
                        await _roleManager.CreateAsync(new IdentityRole(SD.Role_Employee));
                    }
                    if (!await _roleManager.RoleExistsAsync(SD.Role_User_Comp))
                    {
                        await _roleManager.CreateAsync(new IdentityRole(SD.Role_User_Comp));
                    }
                    if (!await _roleManager.RoleExistsAsync(SD.Role_User_Indi))
                    {
                        await _roleManager.CreateAsync(new IdentityRole(SD.Role_User_Indi));
                    }

                    if (user.Role == null)
                    {
                        //if user was not assigned any role, assigned individual user role
                        await _userManager.AddToRoleAsync(user, SD.Role_User_Indi);
                    }
                    else
                    {
                        //if user company ID is greater than 0 then we will assign user to a user company role
                        if (user.CompanyId > 0)
                        {
                            await _userManager.AddToRoleAsync(user, SD.Role_User_Comp);
                        }
                        await _userManager.AddToRoleAsync(user, user.Role);
                    }
                    //await _userManager.AddToRoleAsync(user, SD.Role_Admin);
                    //for right now anyone who registered will be granted admin by default
                    //have to delete or comment this out later on.


                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl },
                        protocol: Request.Scheme);

                    var PathToFile = _hostEnvironment.WebRootPath + Path.DirectorySeparatorChar.
                                     ToString() + "Templates" + Path.DirectorySeparatorChar.
                                     ToString() + "EmailTemplates" + Path.DirectorySeparatorChar.
                                     ToString() +
                                     "Confirm_Account_Registration.html";

                    var    subject  = "Confirm Account Registration";
                    string HtmlBody = "";
                    using (StreamReader streamReader = System.IO.File.OpenText(PathToFile))
                    {
                        HtmlBody = streamReader.ReadToEnd();
                    }

                    //{0} : Subject
                    //{1} : DateTime
                    //{2} : Name
                    //{3} : Email
                    //{4} : Message
                    //{5} : callbackUrl

                    string Message = $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.";

                    string messageBody = string.Format(HtmlBody,
                                                       subject,
                                                       String.Format("{0:dddd, d MMMM yyyy}", DateTime.Now),
                                                       user.Name,
                                                       user.Email,
                                                       Message,
                                                       callbackUrl);

                    await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                      messageBody);

                    if (_userManager.Options.SignIn.RequireConfirmedAccount)
                    {
                        return(RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl }));
                    }
                    else
                    {
                        //if user creating a new account
                        if (user.Role == null)
                        {
                            await _signInManager.SignInAsync(user, isPersistent : false);

                            return(LocalRedirect(returnUrl));
                        }
                        else
                        {
                            //admin is creating a new user so we don't want to log him/her out
                            //and sign in as the newly created user so we redirect him/her to list
                            //of users
                            return(RedirectToAction("Index", "User", new { Area = "Admin" }));
                        }
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }
            Input = new InputModel()
            {
                CompanyList = _unitOfWork.Company.GetAll().Select(i => new SelectListItem
                {
                    Text  = i.Name,
                    Value = i.Id.ToString()
                }),
                //retrieve list to our drop down and pass this to our RazorPage
                RoleList = _roleManager.Roles.Where(u => u.Name != SD.Role_User_Indi).Select(x => x.Name).Select(i => new SelectListItem
                {
                    Text  = i,
                    Value = i
                }),
            };

            // If we got this far, something failed, redisplay form
            return(Page());
        }
Example #2
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl      = returnUrl ?? Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName      = Input.Email,
                    Email         = Input.Email,
                    CompanyId     = Input.CompanyId,
                    StreetAddress = Input.StreetAddress,
                    City          = Input.City,
                    State         = Input.State,
                    PostalCode    = Input.PostalCode,
                    Name          = Input.Name,
                    PhoneNumber   = Input.PhoneNumber,
                    Role          = Input.Role
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");
                    if (user.Role == null)
                    {
                        await _userManager.AddToRoleAsync(user, SD.Role_User_Indi);
                    }
                    else
                    {
                        if (user.CompanyId > 0)
                        {
                            await _userManager.AddToRoleAsync(user, SD.Role_User_Comp);
                        }
                        await _userManager.AddToRoleAsync(user, user.Role);
                    }



                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                    var callbackUrl = Url.Page(
                        "/Account/ConfirmEmail",
                        pageHandler: null,
                        values: new { area = "Identity", userId = user.Id, code = code, returnUrl = returnUrl },
                        protocol: Request.Scheme);

                    await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
                                                      $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");

                    if (_userManager.Options.SignIn.RequireConfirmedAccount)
                    {
                        return(RedirectToPage("RegisterConfirmation", new { email = Input.Email, returnUrl = returnUrl }));
                    }
                    else
                    {
                        if (user.Role == null)
                        {
                            await _signInManager.SignInAsync(user, isPersistent : false);

                            return(LocalRedirect(returnUrl));
                        }
                        else
                        {
                            //admin is registering a new user.
                            return(RedirectToAction("Index", "User", new { Area = "Admin" }));
                        }
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            Input = new InputModel()
            {
                CompanyList = _unitOfWork.Company.GetAll().Select(i => new SelectListItem
                {
                    Text  = i.Name,
                    Value = i.Id.ToString()
                }),
                RoleList = _roleManager.Roles.Where(x => x.Name != SD.Role_User_Indi).Select(x => x.Name).Select(i => new SelectListItem
                {
                    Text  = i,
                    Value = i
                })
            };


            // If we got this far, something failed, redisplay form
            return(Page());
        }
Example #3
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl      = returnUrl ?? Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName      = Input.Email,
                    Email         = Input.Email,
                    CompanyId     = Input.CompanyId,
                    StreetAddress = Input.StreetAddress,
                    City          = Input.City,
                    State         = Input.State,
                    PostalCode    = Input.PostalCode,
                    Name          = Input.Name,
                    PhoneNumber   = Input.PhoneNumber,
                    Role          = Input.Role
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");
                    var users     = _unitOfWork.ApplicationUser.GetAll();
                    int userCount = users.Count();
                    HttpContext.Session.SetInt32("regCount", userCount++);

                    if (user.Role == null)
                    {
                        await _userManager.AddToRoleAsync(user, SD.Role_User_Indi);
                    }
                    else
                    {
                        if (user.CompanyId > 0)
                        {
                            await _userManager.AddToRoleAsync(user, SD.Role_User_Comp);
                        }
                        await _userManager.AddToRoleAsync(user, user.Role);
                    }

                    //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                    //code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code));
                    //var callbackUrl = Url.Page(
                    //    "/Account/ConfirmEmail",
                    //    pageHandler: null,
                    //    values: new { area = "Identity", userId = user.Id, code = code },
                    //    protocol: Request.Scheme);


                    //var PathToFile = _hostEnvironment.WebRootPath + Path.DirectorySeparatorChar.ToString()
                    //    + "Templates" + Path.DirectorySeparatorChar.ToString() + "EmailTemplates"
                    //    + Path.DirectorySeparatorChar.ToString() + "Confirm_Account_Registration.html";

                    //var subject = "Confirm Account Registration";
                    //string HtmlBody = "";
                    //using (StreamReader streamReader = System.IO.File.OpenText(PathToFile))
                    //{
                    //    HtmlBody = streamReader.ReadToEnd();
                    //}

                    //{0} : Subject
                    //{1} : DateTime
                    //{2} : Name
                    //{3} : Email
                    //{4} : Message
                    //{5} : callbackURL

                    //string Message = $"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.";

                    //string messageBody = string.Format(HtmlBody,
                    //    subject,
                    //    String.Format("{0:dddd, d MMMM yyyy}", DateTime.Now),
                    //    user.Name,
                    //    user.Email,
                    //    Message,
                    //    callbackUrl
                    //    );


                    //await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", messageBody);

                    if (_userManager.Options.SignIn.RequireConfirmedAccount)
                    {
                        return(RedirectToPage("RegisterConfirmation", new { email = Input.Email }));
                    }
                    else
                    {
                        if (user.Role == null)
                        {
                            await _signInManager.SignInAsync(user, isPersistent : false);

                            return(LocalRedirect(returnUrl));
                        }
                        else
                        {
                            //admin is registering a new user
                            return(RedirectToAction("Index", "User", new { Area = "Admin" }));
                        }
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }
            Input = new InputModel()
            {
                CompanyList = _unitOfWork.Company.GetAll().Select(i => new SelectListItem
                {
                    Text  = i.Name,
                    Value = i.Id.ToString()
                }),
                RoleList = _roleManager.Roles.Where(u => u.Name != SD.Role_User_Indi).Select(x => x.Name).Select(i => new SelectListItem
                {
                    Text  = i,
                    Value = i
                })
            };

            // If we got this far, something failed, redisplay form
            return(Page());
        }