public static void SignIn(UserManager manager, User user, bool isPersistent)
 {
     IAuthenticationManager authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
     authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
     ClaimsIdentity identity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
     authenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, identity);
 }
 /// <summary>
 /// The create user_ click.
 /// </summary>
 /// <param name="sender">
 /// The sender.
 /// </param>
 /// <param name="e">
 /// The e.
 /// </param>
 protected void CreateUser_Click(object sender, EventArgs e)
 {
     var manager = this.Context.GetOwinContext().GetUserManager<UserManager>();
     var user = new User { UserName = this.Email.Text, Email = this.Email.Text };
     IdentityResult result = manager.Create(user, this.Password.Text);
     if (result.Succeeded)
     {
         // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
         // string code = manager.GenerateEmailConfirmationToken(user.Id);
         // string callbackUrl = IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id, Request);
         // manager.SendEmail(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>.");
         IdentityHelper.SignIn(manager, user, false);
         IdentityHelper.RedirectToReturnUrl(this.Request.QueryString["ReturnUrl"], this.Response);
     }
     else
     {
         this.ErrorMessage.Text = result.Errors.FirstOrDefault();
     }
 }
        /// <summary>
        /// The create and login user.
        /// </summary>
        private void CreateAndLoginUser()
        {
            if (!this.IsValid)
            {
                return;
            }

            var manager = this.Context.GetOwinContext().GetUserManager<UserManager>();
            var user = new User { UserName = this.email.Text, Email = this.email.Text };
            IdentityResult result = manager.Create(user);
            if (result.Succeeded)
            {
                ExternalLoginInfo loginInfo = this.Context.GetOwinContext().Authentication.GetExternalLoginInfo();
                if (loginInfo == null)
                {
                    this.RedirectOnFail();
                    return;
                }

                result = manager.AddLogin(user.Id, loginInfo.Login);
                if (result.Succeeded)
                {
                    IdentityHelper.SignIn(manager, user, false);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // var code = manager.GenerateEmailConfirmationToken(user.Id);
                    // Send this link via email: IdentityHelper.GetUserConfirmationRedirectUrl(code, user.Id)
                    IdentityHelper.RedirectToReturnUrl(this.Request.QueryString["ReturnUrl"], this.Response);
                    return;
                }
            }

            this.AddErrors(result);
        }