Exemple #1
0
        private static async Task SeedDefaultUsersAsync(UserManager <BTUser> userManager, IImageService imageService)
        {
            #region Seed Admin

            var defaultAdmin = new BTUser
            {
                UserName       = "******",
                Email          = "*****@*****.**",
                FirstName      = "Charlie",
                LastName       = "Tincher",
                FileName       = "defaultavatar.jpg",
                FileData       = await imageService.AssignAvatarAsync("defaultavatar.jpg"),
                EmailConfirmed = true
            };
            try
            {
                var user = await userManager.FindByEmailAsync(defaultAdmin.Email);

                if (user == null)
                {
                    await userManager.CreateAsync(defaultAdmin, "Charlie@123");

                    await userManager.AddToRoleAsync(defaultAdmin, Roles.Admin.ToString());
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("************ ERROR ************");
                Debug.WriteLine("Error Seeding Default Admin User.");
                Debug.WriteLine("ex.Message");
                Debug.WriteLine("*******************************");
            }
            #endregion

            #region Seed Project Manager

            var defaultProjectManager = new BTUser
            {
                UserName       = "******",
                Email          = "*****@*****.**",
                FirstName      = "DustinPM",
                LastName       = "Kensrue",
                FileName       = "defaultavatar.jpg",
                FileData       = await imageService.AssignAvatarAsync("defaultavatar.jpg"),
                EmailConfirmed = true
            };
            try
            {
                var user = await userManager.FindByEmailAsync(defaultProjectManager.Email);

                if (user == null)
                {
                    await userManager.CreateAsync(defaultProjectManager, "Kensrue@123");

                    await userManager.AddToRoleAsync(defaultProjectManager, Roles.ProjectManager.ToString());
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("************ ERROR ************");
                Debug.WriteLine("Error Seeding Default Project Manager User.");
                Debug.WriteLine("ex.Message");
                Debug.WriteLine("*******************************");
            }

            defaultProjectManager = new BTUser
            {
                UserName       = "******",
                Email          = "*****@*****.**",
                FirstName      = "JakePM",
                LastName       = "Smith",
                EmailConfirmed = true
            };
            try
            {
                var user = await userManager.FindByEmailAsync(defaultProjectManager.Email);

                if (user == null)
                {
                    await userManager.CreateAsync(defaultProjectManager, "Jake@123");

                    await userManager.AddToRoleAsync(defaultProjectManager, Roles.ProjectManager.ToString());
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("************ ERROR ************");
                Debug.WriteLine("Error Seeding Default Project Manager User.");
                Debug.WriteLine("ex.Message");
                Debug.WriteLine("*******************************");
            }
            #endregion

            #region Seed Developer

            var defaultDeveloper = new BTUser
            {
                UserName       = "******",
                Email          = "*****@*****.**",
                FirstName      = "BobbyDev",
                LastName       = "Long",
                FileName       = "defaultavatar.jpg",
                FileData       = await imageService.AssignAvatarAsync("defaultavatar.jpg"),
                EmailConfirmed = true
            };
            try
            {
                var user = await userManager.FindByEmailAsync(defaultDeveloper.Email);

                if (user == null)
                {
                    await userManager.CreateAsync(defaultDeveloper, "Bobby@123");

                    await userManager.AddToRoleAsync(defaultDeveloper, Roles.Developer.ToString());
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("************ ERROR ************");
                Debug.WriteLine("Error Seeding Default Developer User.");
                Debug.WriteLine(ex.Message);
                Debug.WriteLine("*******************************");
            }
            #endregion

            #region Seed Submitter

            var defaultSubmitter = new BTUser
            {
                UserName       = "******",
                Email          = "*****@*****.**",
                FirstName      = "Chris",
                LastName       = "Cornell",
                FileName       = "defaultavatar.jpg",
                FileData       = await imageService.AssignAvatarAsync("defaultavatar.jpg"),
                EmailConfirmed = true
            };
            try
            {
                var user = await userManager.FindByEmailAsync(defaultSubmitter.Email);

                if (user == null)
                {
                    await userManager.CreateAsync(defaultSubmitter, "Chris@123");

                    await userManager.AddToRoleAsync(defaultSubmitter, Roles.Submitter.ToString());
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("************ ERROR ************");
                Debug.WriteLine("Error Seeding Default Submitter User.");
                Debug.WriteLine("ex.Message");
                Debug.WriteLine("*******************************");
            }
            #endregion

            #region Seed New User

            var defaultNewUser = new BTUser
            {
                UserName       = "******",
                Email          = "*****@*****.**",
                FirstName      = "James",
                LastName       = "Taylor",
                FileName       = "defaultavatar.jpg",
                FileData       = await imageService.AssignAvatarAsync("defaultavatar.jpg"),
                EmailConfirmed = true
            };
            try
            {
                var user = await userManager.FindByEmailAsync(defaultNewUser.Email);

                if (user == null)
                {
                    await userManager.CreateAsync(defaultNewUser, "James@123");

                    await userManager.AddToRoleAsync(defaultNewUser, Roles.NewUser.ToString());
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("************ ERROR ************");
                Debug.WriteLine("Error Seeding Default New User.");
                Debug.WriteLine("ex.Message");
                Debug.WriteLine("*******************************");
            }
            #endregion

            //Start demo user seeds
            //Each user occupies a "main" role and this new demo version of the role
            //We will target this Demo role in order to demo this project without giving the abilty to this demo users to access the database
            //Below is the password for all of the demo users
            string demoPassword = "******";
            //Anthony Stark
            #region Demo Seed Admin

            defaultAdmin = new BTUser
            {
                UserName       = "******",
                Email          = "*****@*****.**",
                FirstName      = "Anthony",
                LastName       = "Stark",
                FileName       = "stark.jpg",
                FileData       = await imageService.AssignAvatarAsync("stark.jpg"),
                EmailConfirmed = true
            };
            try
            {
                var user = await userManager.FindByEmailAsync(defaultAdmin.Email);

                if (user == null)
                {
                    await userManager.CreateAsync(defaultAdmin, demoPassword);

                    await userManager.AddToRoleAsync(defaultAdmin, Roles.Admin.ToString());

                    await userManager.AddToRoleAsync(defaultAdmin, Roles.Demo.ToString());
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("************ ERROR ************");
                Debug.WriteLine("Error Seeding Default Admin User.");
                Debug.WriteLine("ex.Message");
                Debug.WriteLine("*******************************");
            }
            #endregion
            //Harley Keener
            #region Demo Seed Project Manager

            defaultProjectManager = new BTUser
            {
                UserName       = "******",
                Email          = "*****@*****.**",
                FirstName      = "Harley",
                LastName       = "Keener",
                FileName       = "harley-keener.jpg",
                FileData       = await imageService.AssignAvatarAsync("harley-keener.jpg"),
                EmailConfirmed = true
            };
            try
            {
                var user = await userManager.FindByEmailAsync(defaultProjectManager.Email);

                if (user == null)
                {
                    await userManager.CreateAsync(defaultProjectManager, demoPassword);

                    await userManager.AddToRoleAsync(defaultProjectManager, Roles.ProjectManager.ToString());

                    await userManager.AddToRoleAsync(defaultProjectManager, Roles.Demo.ToString());
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("************ ERROR ************");
                Debug.WriteLine("Error Seeding Default Project Manager User.");
                Debug.WriteLine("ex.Message");
                Debug.WriteLine("*******************************");
            }

            //defaultProjectManager = new BTUser
            //{
            //    UserName = "******",
            //    Email = "*****@*****.**",
            //    FirstName = "JakeDemo",
            //    LastName = "Smith",
            //    EmailConfirmed = true
            //};
            //try
            //{
            //    var user = await userManager.FindByEmailAsync(defaultProjectManager.Email);
            //    if (user == null)
            //    {
            //        await userManager.CreateAsync(defaultProjectManager, demoPassword);
            //        await userManager.AddToRoleAsync(defaultProjectManager, Roles.ProjectManager.ToString());
            //        await userManager.AddToRoleAsync(defaultProjectManager, Roles.Demo.ToString());
            //    }
            //}
            //catch (Exception ex)
            //{
            //    Debug.WriteLine("************ ERROR ************");
            //    Debug.WriteLine("Error Seeding Default Project Manager User.");
            //    Debug.WriteLine("ex.Message");
            //    Debug.WriteLine("*******************************");

            //}

            #endregion
            //Carol Danvers
            #region Demo Seed Developer

            defaultDeveloper = new BTUser
            {
                UserName       = "******",
                Email          = "*****@*****.**",
                FirstName      = "Carol",
                LastName       = "Danvers",
                FileName       = "carol.png",
                FileData       = await imageService.AssignAvatarAsync("carol.png"),
                EmailConfirmed = true
            };
            try
            {
                var user = await userManager.FindByEmailAsync(defaultDeveloper.Email);

                if (user == null)
                {
                    await userManager.CreateAsync(defaultDeveloper, demoPassword);

                    await userManager.AddToRoleAsync(defaultDeveloper, Roles.Developer.ToString());

                    await userManager.AddToRoleAsync(defaultDeveloper, Roles.Demo.ToString());
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("************ ERROR ************");
                Debug.WriteLine("Error Seeding Default Developer User.");
                Debug.WriteLine(ex.Message);
                Debug.WriteLine("*******************************");
            }
            #endregion
            //Clint Barton
            #region Demo Seed Submitter

            defaultSubmitter = new BTUser
            {
                UserName       = "******",
                Email          = "*****@*****.**",
                FirstName      = "Clint",
                LastName       = "Barton",
                FileName       = "clint.jpg",
                FileData       = await imageService.AssignAvatarAsync("clint.jpg"),
                EmailConfirmed = true
            };
            try
            {
                var user = await userManager.FindByEmailAsync(defaultSubmitter.Email);

                if (user == null)
                {
                    await userManager.CreateAsync(defaultSubmitter, demoPassword);

                    await userManager.AddToRoleAsync(defaultSubmitter, Roles.Submitter.ToString());

                    await userManager.AddToRoleAsync(defaultSubmitter, Roles.Demo.ToString());
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("************ ERROR ************");
                Debug.WriteLine("Error Seeding Default Submitter User.");
                Debug.WriteLine("ex.Message");
                Debug.WriteLine("*******************************");
            }
            #endregion
            //James Gunn
            #region Demo Seed New User

            defaultNewUser = new BTUser
            {
                UserName       = "******",
                Email          = "*****@*****.**",
                FirstName      = "James",
                LastName       = "Gunn",
                FileName       = "james.jpg",
                FileData       = await imageService.AssignAvatarAsync("james.jpg"),
                EmailConfirmed = true
            };
            try
            {
                var user = await userManager.FindByEmailAsync(defaultNewUser.Email);

                if (user == null)
                {
                    await userManager.CreateAsync(defaultNewUser, demoPassword);

                    await userManager.AddToRoleAsync(defaultNewUser, Roles.NewUser.ToString());

                    await userManager.AddToRoleAsync(defaultNewUser, Roles.Demo.ToString());
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("************ ERROR ************");
                Debug.WriteLine("Error Seeding Default New User.");
                Debug.WriteLine("ex.Message");
                Debug.WriteLine("*******************************");
            }
            #endregion
        }
Exemple #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 BTUser
                {
                    FirstName = Input.FirstName,
                    LastName  = Input.LastName,
                    UserName  = Input.Email,
                    Email     = Input.Email,
                    FileName  = "defaultavatar.jpg",
                    FileData  = await _imageService.AssignAvatarAsync("defaultavatar.jpg")
                };

                if (Input.FormFile != null)
                {
                    user.FileName = Input.FormFile.FileName;
                    user.FileData = _imageService.ConvertFileToByteArray(Input.FormFile);
                }
                ;

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

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");
                    await _userManager.AddToRoleAsync(user, Roles.NewUser.ToString());

                    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)
                    {
                        TempData["ConfirmEmail"] = "Please verify your email";
                        return(RedirectToPage("/Account/Login", new { area = "Identity" }));
                    }
                    else
                    {
                        await _signInManager.SignInAsync(user, isPersistent : false);

                        return(LocalRedirect(returnUrl));
                    }
                }
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError(string.Empty, error.Description);
                }
            }

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