public void WriteUpdatesTo(ApplicationUser user)
 {
     user.Email = Email;
     user.EmailConfirmed = EmailConfirmed;
     user.Activated = Activated ? ActivationStatus.Activated : ActivationStatus.Rejected;
     user.UserName = Email;
 }
 public static EditUserViewModel From(ApplicationUser user)
 {
     return new EditUserViewModel {
         Id = user.Id,
         Email= user.Email,
         EmailConfirmed = user.EmailConfirmed,
         Activated = user.Activated == ActivationStatus.Activated
     };
 }
 public static ActivateUserViewModel From(ApplicationUser user, List<string> roles)
 {
     return new ActivateUserViewModel
     {
         Id = user.Id,
         Email= user.Email,
         EmailConfirmed = user.EmailConfirmed,
        Roles = roles
     };
 }
        private static object CreateUserViewRows(ApplicationUser user, IEnumerable<IdentityRole> roles, IEnumerable<UserGroup> groups)
        {
            var roleTexts = user.Roles.Join(roles, o => o.RoleId, i => i.Id, (r, i) => i.Name);
            var groupTexts = groups.Select(g => g.Name);

            return new [] 
            {
                user.Id,
                user.UserName,
                user.EmailConfirmed.ToString(),
                user.EmailConfirmed ? user.Activated.GetDisplayName() : string.Empty,
                string.Join(", ", roleTexts),
                string.Join(", ", groupTexts),
                string.IsNullOrWhiteSpace(user.PasswordHash) ? string.Empty : "******",
                user.AccessFailedCount.ToString()
            };
        }
 private static object CreateActivityViewRows(UserActivityLogEntry entry, ApplicationUser user)
 {
     return new [] 
     {
         user.Id,
         entry.TimeStamp.ToString("yyyy-MM-dd hh:mm"),
         user.UserName,
         entry.ActivityKind.Caption(),
         entry.ObjectKind.Caption()
     };
 }
        private async Task OptToNotifyUponChanges(IEnumerable<string> toRemove, IEnumerable<RoleViewModel> toAdd, ApplicationUser affectedUser)
        {
            var removalsToFlag = toRemove.Where(r => SoniRoles.Managers.Any()).ToList();
            var addsToFlag = toAdd.Select(m => m.RoleName).Where(a => SoniRoles.Managers.Any(m => m == a)).ToList();

            if (removalsToFlag.Count > 0 || addsToFlag.Count > 0)
            {
                var body = string.Format("Bruger {0} har foretaget følgende ændringer i brugerrettigheder for bruger {1}: Tilføjede rolle(r): {2}. Fjernede rolle(r): {3}", 
                    User.Identity.Name, affectedUser.UserName, 
                    removalsToFlag.Count == 0 ? "(Ingen)" : string.Join(", ", removalsToFlag),
                    addsToFlag.Count == 0 ? "(Ingen)" : string.Join(", ", addsToFlag));

                var recipient = GetRoleChangeNotificationRecipient();

                await UserManager.SendEmailAsync(recipient.Id, "Ændring i brugerrettigheder", body);
            }   
        }
        public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Index", "Manage");
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    return View("ExternalLoginFailure");
                }
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                        return RedirectToLocal(returnUrl);
                    }
                }
                AddErrorsToModelState(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
        protected async Task CreateEmailConfirmation(ApplicationUser user)
        {
            var 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, "Bekræft din emailadresse",
                "Bekræft venligst din emailadresse med ved at klikke på dette link <a href=\"" +
                callbackUrl + "\">Bekræft</a>. Bemærk: Denne email kan ikke besvares.");

        }
        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)
                {
                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    await CreateEmailConfirmation(user);
                    return View("EmailMustBeConfirmed");
                }
                AddErrorsToModelState(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
 internal static AssignGroupsViewModel From(ApplicationUser user, List<GroupViewModel> groupList)
 {
     return new AssignGroupsViewModel { UserId = user.Id, UserName = user.UserName, UserGroups = groupList };
 }
 internal static AssignRolesViewModel From(ApplicationUser user, List<RoleViewModel> roleList)
 {
     return new AssignRolesViewModel { UserId = user.Id, UserName = user.UserName, Roles = roleList };
 }