示例#1
0
        public async Task <ActionResult> AddUser(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                AdminServices   adminServices = new AdminServices();
                RandomGenerator rg            = new RandomGenerator();
                var             password      = "******";

                var UserManager = HttpContext.GetOwinContext().GetUserManager <ApplicationUserManager>();
                var username    = model.FirstName[0] + model.LastName + rg.GenerateRandomNo();

                var usere = adminServices.getByEmail(model.Email);
                if (usere != null)
                {
                    ModelState.AddModelError("", "Email already exists!");
                    return(View(model));
                }

                while (adminServices.getByUsername(username) != null)
                {
                    username = model.FirstName[0] + model.LastName + rg.GenerateRandomNo();
                }

                var user = new ApplicationUser {
                    UserName = username, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, password);

                if (result.Succeeded)
                {
                    // await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    adminServices.AddUserInfo(model.FirstName, model.LastName, model.MiddleName, user.Id, true);

                    return(RedirectToAction("username", "Home", new { username = username, password = password }));
                }
                AddErrors(result);
            }

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