Ejemplo n.º 1
0
        public static async Task AdminSeed(IServiceProvider serviceProvider)
        {
            var RoleManager = serviceProvider.GetRequiredService <RoleManager <IdentityRole> >();
            var UserManager = serviceProvider.GetRequiredService <UserManager <BankDbUser> >();

            string[] roleNames = { "Admin", "Customer" };

            IdentityResult roleResult;

            foreach (var roleName in roleNames)
            {
                var roleExist = await RoleManager.RoleExistsAsync(roleName);

                if (!roleExist)
                {
                    //create the roles and seed them to the database: Question 1
                    roleResult = await RoleManager.CreateAsync(new IdentityRole(roleName));
                }
            }

            var poweruser = new BankDbUser
            {
                UserName       = "******",
                Email          = "*****@*****.**",
                EmailConfirmed = true
            };

            //Ensure you have these values in your appsettings.json file
            string userPWD = "Abc123!";
            var    _user   = await UserManager.FindByEmailAsync(poweruser.Email);

            if (_user == null)
            {
                var createPowerUser = await UserManager.CreateAsync(poweruser, userPWD);

                if (createPowerUser.Succeeded)
                {
                    //here we tie the new user to the role
                    await UserManager.AddToRoleAsync(poweruser, "Admin");
                }
            }


            var newUser = new BankDbUser
            {
                UserName       = "******",
                Email          = "*****@*****.**",
                EmailConfirmed = true,
                CustomerID     = 2100
            };


            //Ensure you have these values in your appsettings.json file
            string userPWD1 = "Abc123!";
            var    _user1   = await UserManager.FindByEmailAsync(newUser.Email);

            if (_user1 == null)
            {
                var createNewUser = await UserManager.CreateAsync(newUser, userPWD1);

                if (createNewUser.Succeeded)
                {
                    //here we tie the new user to the role
                    await UserManager.AddToRoleAsync(newUser, "Customer");
                }
            }

            var newUser1 = new BankDbUser
            {
                UserName       = "******",
                Email          = "*****@*****.**",
                EmailConfirmed = true,
                CustomerID     = 2200
            };

            //Ensure you have these values in your appsettings.json file
            string userPWD2 = "Abc123!";
            var    _user2   = await UserManager.FindByEmailAsync(newUser1.Email);

            if (_user2 == null)
            {
                var createNewUser = await UserManager.CreateAsync(newUser1, userPWD2);

                if (createNewUser.Succeeded)
                {
                    //here we tie the new user to the role
                    await UserManager.AddToRoleAsync(newUser1, "Customer");
                }
            }

            var newUser3 = new BankDbUser
            {
                UserName       = "******",
                Email          = "*****@*****.**",
                EmailConfirmed = true,
                CustomerID     = 2300
            };

            //Ensure you have these values in your appsettings.json file
            string userPWD3 = "Abc123!";
            var    _user3   = await UserManager.FindByEmailAsync(newUser3.Email);

            if (_user3 == null)
            {
                var createNewUser2 = await UserManager.CreateAsync(newUser3, userPWD3);

                if (createNewUser2.Succeeded)
                {
                    //here we tie the new user to the role
                    await UserManager.AddToRoleAsync(newUser3, "Customer");
                }
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> OnPostAsync(string returnUrl = null)
        {
            returnUrl ??= Url.Content("~/");
            ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();

            if (Input.AccountType == AccountType.Checking && Input.Amount < 500)
            {
                ModelState.AddModelError("Amount", "The minimum balance for checking account is $500");
            }
            if (Input.AccountType == AccountType.Saving && Input.Amount < 100)
            {
                ModelState.AddModelError("Amount", "The minimum balance for saving account is $100");
            }

            if (ModelState.IsValid)
            {
                var user = new BankDbUser {
                    UserName = Input.Email, Email = Input.Email
                };
                var result = await _userManager.CreateAsync(user, Input.Password);

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

                    //Create a new Customer
                    var bankUser = _context.Users.Where(x => x.Email == Input.Email).FirstOrDefault();

                    //for testing purposes
                    int    _min = 1000;
                    int    _max = 9999;
                    Random _rdm = new Random();

                    bankUser.Customer = new Customer()
                    {
                        CustomerID   = _rdm.Next(_min, _max),
                        CustomerName = Input.Name,
                        Phone        = "+6112345678",
                        Status       = CustomerStatus.Unlocked,
                        Accounts     = new List <s3827202_s3687609_a2.Areas.Banking.Models.Account>()
                        {
                            new s3827202_s3687609_a2.Areas.Banking.Models.Account
                            {
                                AccountNumber   = _rdm.Next(_min, _max),
                                AccountType     = Input.AccountType,
                                Balance         = Input.Amount,
                                ModifyDate      = DateTime.Now,
                                FreeTransaction = 4,
                                Transactions    = new List <Transaction>()
                                {
                                    new Transaction()
                                    {
                                        TransactionType   = TransactionType.Deposit,
                                        Amount            = Input.Amount,
                                        Comment           = "Initial Deposit",
                                        TransactionStatus = TransactionStatus.Idle
                                    }
                                }
                            }
                        }
                    };

                    await _context.SaveChangesAsync();

                    //bankUser.CustomerID = bankUser.Customer.CustomerID;



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