コード例 #1
0
        public async Task <IActionResult> Accept(string email, string code, string firstName, string lastName, IFormFile avatar, string password)
        {
            var invitation = await _context.Invitation.FirstOrDefaultAsync(i => i.Code.ToString() == code);

            byte[] fileData;
            string fileName;

            if (avatar != null)
            {
                fileData = await _fileService.ConvertFileToByteArrayAsync(avatar);

                fileName = avatar.FileName;
            }
            else
            {
                fileData = await _fileService.GetDefaultAvatarFileBytesAsync();

                fileName = _fileService.GetDefaultAvatarFileName();
            }
            var user = new FPUser
            {
                FirstName      = firstName,
                LastName       = lastName,
                FileName       = fileName,
                FileData       = fileData,
                UserName       = email,
                Email          = email,
                HouseHoldId    = invitation.HouseHoldId,
                EmailConfirmed = true
            };

            invitation.Accepted = true;
            // where the magic happens
            await _userManager.CreateAsync(user, password);

            await _userManager.AddToRoleAsync(user, Roles.Member.ToString());

            await _signInManager.SignInAsync(user, false);

            await _context.SaveChangesAsync();

            return(RedirectToAction("Dashboard", "HouseHolds"));
        }
コード例 #2
0
        private static async Task SeedUsersAsync(UserManager <FPUser> userManager, IFPFileService fileService, IConfiguration configuration, ApplicationDbContext context)
        {
            #region Admin
            var user = new FPUser();
            user.UserName  = "******";
            user.Email     = "*****@*****.**";
            user.FirstName = "Mackenzie";
            user.LastName  = "Weaver";
            user.FileData  = await fileService.GetDefaultAvatarFileBytesAsync();

            user.FileName       = fileService.GetDefaultAvatarFileName();
            user.EmailConfirmed = true;
            try
            {
                var test = await userManager.FindByEmailAsync(user.Email);

                if (test == null)
                {
                    await userManager.CreateAsync(user, "Mweaver1!");

                    await userManager.AddToRoleAsync(user, Roles.Admin.ToString());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("========= ERROR ============");
                Console.WriteLine($"Error Seeding {user.Email}");
                Console.WriteLine(ex.Message);
                Console.WriteLine("============================");
                throw;
            }
            #endregion

            #region Head
            user = new FPUser
            {
                UserName       = "******",
                Email          = "*****@*****.**",
                FirstName      = "Antonio",
                LastName       = "Raynor",
                FileData       = await fileService.GetDefaultAvatarFileBytesAsync(),
                FileName       = fileService.GetDefaultAvatarFileName(),
                EmailConfirmed = true
            };
            try
            {
                var test = await userManager.FindByEmailAsync(user.Email);

                if (test == null)
                {
                    await userManager.CreateAsync(user, "Araynor1!");

                    await userManager.AddToRoleAsync(user, Roles.Head.ToString());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("========= ERROR ============");
                Console.WriteLine($"Error Seeding {user.Email}");
                Console.WriteLine(ex.Message);
                Console.WriteLine("============================");
                throw;
            }
            #endregion

            #region Member
            user = new FPUser
            {
                UserName       = "******",
                Email          = "*****@*****.**",
                FirstName      = "Jason",
                LastName       = "Twichell",
                FileData       = await fileService.GetDefaultAvatarFileBytesAsync(),
                FileName       = fileService.GetDefaultAvatarFileName(),
                EmailConfirmed = true
            };
            try
            {
                var test = await userManager.FindByEmailAsync(user.Email);

                if (test == null)
                {
                    await userManager.CreateAsync(user, "Jtwichell1!");

                    await userManager.AddToRoleAsync(user, Roles.Member.ToString());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("========= ERROR ============");
                Console.WriteLine($"Error Seeding {user.Email}");
                Console.WriteLine(ex.Message);
                Console.WriteLine("============================");
                throw;
            }
            #endregion

            #region New
            user = new FPUser
            {
                UserName       = "******",
                Email          = "*****@*****.**",
                FirstName      = "Drew",
                LastName       = "Russell",
                FileData       = await fileService.GetDefaultAvatarFileBytesAsync(),
                FileName       = fileService.GetDefaultAvatarFileName(),
                EmailConfirmed = true
            };
            try
            {
                var test = await userManager.FindByEmailAsync(user.Email);

                if (test == null)
                {
                    await userManager.CreateAsync(user, "Drussell1!");

                    await userManager.AddToRoleAsync(user, Roles.New.ToString());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("========= ERROR ============");
                Console.WriteLine($"Error Seeding {user.Email}");
                Console.WriteLine(ex.Message);
                Console.WriteLine("============================");
                throw;
            }
            #endregion

            #region Demo
            user           = new FPUser();
            user.UserName  = configuration["DemoEmail"];
            user.Email     = configuration["DemoEmail"];
            user.FirstName = "Rock";
            user.LastName  = "Demo";
            user.FileData  = await fileService.GetDefaultAvatarFileBytesAsync();

            user.FileName       = fileService.GetDefaultAvatarFileName();
            user.EmailConfirmed = true;
            try
            {
                var test = await userManager.FindByEmailAsync(user.Email);

                if (test == null)
                {
                    await userManager.CreateAsync(user, configuration["DemoPassword"]);

                    await userManager.AddToRoleAsync(user, Roles.Head.ToString());

                    await userManager.AddToRoleAsync(user, Roles.Demo.ToString());
                    await SeedAccount(user, context);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("========= ERROR ============");
                Console.WriteLine($"Error Seeding {user.Email}");
                Console.WriteLine(ex.Message);
                Console.WriteLine("============================");
                throw;
            }
            #endregion
        }
コード例 #3
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl      = returnUrl ?? Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            if (ModelState.IsValid)
            {
                byte[] fileData;
                string fileName;
                if (Input.Avatar != null)
                {
                    fileData = await _fileService.ConvertFileToByteArrayAsync(Input.Avatar);

                    fileName = Input.Avatar.FileName;
                }
                else
                {
                    fileData = await _fileService.GetDefaultAvatarFileBytesAsync();

                    fileName = _fileService.GetDefaultAvatarFileName();
                }
                var user = new FPUser
                {
                    FirstName = Input.FirstName,
                    LastName  = Input.LastName,
                    FileName  = fileName,
                    FileData  = fileData,
                    UserName  = Input.Email,
                    Email     = Input.Email
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

                await _userManager.AddToRoleAsync(user, Roles.New.ToString());

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    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
                    {
                        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());
        }