示例#1
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    user.City             = model.City;
                    user.FirstName        = model.FirstName;
                    user.FlatNumber       = model.FlatNumber;
                    user.HouseNumber      = model.HouseNumber;
                    user.LastName         = model.LastName;
                    user.Street           = model.Street;
                    user.ZipCode          = model.ZipCode;
                    user.CreateTime       = DateTime.Now;
                    user.TwoFactorEnabled = true;
                    user.ModyfiTime       = DateTime.Now;
                    var currentUser      = UserManager.FindByName(user.UserName);
                    var role             = UserManager.AddToRole(currentUser.Id, "Administrator".ToUpper());
                    DatabaseControler dc = new DatabaseControler();
                    dc.UpdateUserData(user, currentUser.Id);
                    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, "Potwierdź konto", "Kliknij <a href=\"" + callbackUrl + "\">link</a>, aby potwierdzić utworzenie konta.");

                    return(RedirectToAction("AddPhoneNumber", "Manage", new { Message = AccountMessageId.RegisterAccountSuccess }));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
示例#2
0
        public ActionResult Save(DriverViewModels model)
        {
            if (!ModelState.IsValid)
            {
                return(View("DriverForm", model));
            }
            else if (!IsAdministrator())
            {
                RedirectToAction("Index", "Home");
            }

            DatabaseControler      dc          = new DatabaseControler();
            IdentityResult         result      = IdentityResult.Success;
            ApplicationUser        user        = new ApplicationUser();
            ApplicationUserManager userManager = HttpContext.GetOwinContext().GetUserManager <ApplicationUserManager>();

            if (string.IsNullOrEmpty(model.Id))
            {
                user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                result = userManager.Create(user, model.Password);
                if (result.Succeeded)
                {
                    user.City                 = model.City;
                    user.FirstName            = model.FirstName;
                    user.FlatNumber           = model.FlatNumber;
                    user.HouseNumber          = model.HouseNumber;
                    user.LastName             = model.LastName;
                    user.Street               = model.Street;
                    user.ZipCode              = model.ZipCode;
                    user.CreateTime           = DateTime.Now;
                    user.ModyfiTime           = DateTime.Now;
                    user.EmailConfirmed       = true;
                    user.PhoneNumberConfirmed = true;
                    user.PhoneNumber          = "+48" + model.Number;
                    user.TwoFactorEnabled     = true;
                    var currentUser = userManager.FindByName(user.UserName);
                    var role        = userManager.AddToRole(currentUser.Id, "Kierowca".ToUpper());
                }
                AddErrors(result);
            }
            else
            {
                user             = dc.GetUserById(model.Id);
                user.City        = model.City;
                user.FirstName   = model.FirstName;
                user.FlatNumber  = model.FlatNumber;
                user.HouseNumber = model.HouseNumber;
                user.LastName    = model.LastName;
                user.Street      = model.Street;
                user.ZipCode     = model.ZipCode;
                user.CreateTime  = DateTime.Now;
                user.ModyfiTime  = DateTime.Now;
                user.Email       = model.Email;
                user.ModyfiTime  = DateTime.Now;
                user.PhoneNumber = model.Number;
            }

            if (result.Succeeded)
            {
                dc.UpdateUserData(user, User.Identity.GetUserId());
            }
            return(RedirectToAction("Index", "Drivers"));
        }