// Enable initialization with an instance of ApplicationUser:
        public SelectUserRolesViewModel(ApplicationUser user)
            : this()
        {
            var db = new ApplicationContext();

            // Add all available roles to the list of EditorViewModels:
            var findUser = db.Users.First(u=>u.Email == user.Email);
            if (findUser != null)
            {
                Id = findUser.Id;
                Email = findUser.Email;
                FirstName = findUser.FirstName;
                UserName = findUser.UserName;
                LastName = findUser.LastName;
                Address = findUser.Address;
                City = findUser.City;
                Province = findUser.Province;
            }
            var allRoles = db.Roles;
            foreach (var role in allRoles)
            {
                // An EditorViewModel will be used by Editor Template:
                var rvm = new SelectRoleEditorViewModel(role);
                Roles.Add(rvm);
            }

            // Set the Selected property to true for those roles for
            // which the current user is a member:
            foreach (var userRole in user.Roles)
            {
                var checkUserRole =
                    Roles.Find(r => r.RoleName == userRole.Role.Name);
                checkUserRole.Selected = true;
            }
        }
Beispiel #2
0
 public bool CreateUser(ApplicationUser user, string password)
 {
     var um = new UserManager<ApplicationUser>(
         new UserStore<ApplicationUser>(new ApplicationContext()));
     um.UserValidator = new UserValidator<ApplicationUser>(um) { AllowOnlyAlphanumericUserNames = false };
     IdentityResult idResult = um.Create(user, password);
     return idResult.Succeeded;
 }
 // Allow Initialization with an instance of ApplicationUser:
 public EditUserViewModel(ApplicationUser user)
 {
     Id = user.Id;
     UserName = user.UserName;
     FirstName = user.FirstName;
     LastName = user.LastName;
     Address = user.Address;
     City = user.City;
     Province = user.Province;
     PostalCode = user.PostalCode;
     Phone = user.Phone;
     Email = user.Email;
 }
Beispiel #4
0
 public void DeleteUser(ApplicationUser user)
 {
     _accountRepository.Remove(user);
 }
Beispiel #5
0
 public void UpdateUserInfo(ApplicationUser user)
 {
     _accountRepository.Update(user);
 }
 private async Task SignInAsync(ApplicationUser user, bool isPersistent)
 {
     AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
     ClaimsIdentity identity =
         await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
     AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, identity);
 }
        public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model,
            string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                ExternalLoginInfo info = await AuthenticationManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    return View("ExternalLoginFailure");
                }
                var user = new ApplicationUser
                           {
                               UserName = model.Email,
                               FirstName = model.FirstName,
                               LastName = model.LastName,
                               Address = model.Address,
                               City = model.City,
                               PostalCode = model.PostalCode,
                               Province = model.Province,
                               Phone = model.Phone,
                               Email = model.Email
                           };

                IdentityResult result = await UserManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        UserManager.AddToRole(user.Id, "Customer");
                        await SignInAsync(user, false);
                        return RedirectToLocal(returnUrl);
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                           {
                               UserName = model.Email,
                               FirstName = model.FirstName,
                               LastName = model.LastName,
                               Address = model.Address,
                               City = model.City,
                               PostalCode = model.PostalCode,
                               Province = model.Province,
                               Phone = model.Phone,
                               Email = model.Email
                           };
                try
                {
                    IdentityResult result = await UserManager.CreateAsync(user, model.Password);
                    if (result.Succeeded)
                    {
                        await SignInAsync(user, false);

                        UserManager.AddToRole(user.Id,
                            string.IsNullOrEmpty(model.RoleName) ? "Customer" : model.RoleName);

                        var url = string.Concat(ApplicationPath.GetSiteRoot(), Url.Action("Manage", "Account"));

                        return RedirectToAction("Index", "User");
                    }
                    AddErrors(result);
                }
                catch
                {
                }
            }

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