void When(UserCreated evnt)
 {
     if (_users.ContainsKey(evnt.UserId.ToString()))
         return;
     var s = new ApplicationUser();
     s.Apply(evnt);
     _users.Add(evnt.UserId.ToString(), s);
 }
 public async Task<IdentityResult> CreateAsync(ApplicationUser user)
 {
     var h = MvcApplication.CommandManager.Create<AddUser>();
     return TryExecute(() => h(new AddUser
     {
         Id = Guid.NewGuid(),
         Timestamp = DateTime.Now,
         UserId = user.Id,
         UserName = user.UserName,
     }));
 }
        public virtual async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser() { UserName = model.UserName, Id = UserId.CreateNew().ToString() };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInAsync(user, isPersistent: false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    AddErrors(result);
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
        public async Task<ClaimsIdentity> CreateIdentityAsync(ApplicationUser user, string applicationCookie)
        {
            var t = new System.Security.Principal.GenericIdentity(user.UserName, applicationCookie);
            var c2 = new Claim("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", user.Id + "");
            var c1 = new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity");

            return new ClaimsIdentity(t, new[] { c1, c2 });
        }
 async Task SignInAsync(ApplicationUser user, bool isPersistent)
 {
     isPersistent = true;
     AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
     var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
     AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
 }
        public virtual 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
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    return View("ExternalLoginFailure");
                }
                var user = new ApplicationUser() { UserName = model.UserName, Id = UserId.CreateNew().ToString() };
                var result = await UserManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        await SignInAsync(user, isPersistent: false);
                        return RedirectToLocal(returnUrl);
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }