Exemplo n.º 1
0
        protected void CreateUser_Click(object sender, EventArgs e)
        {
            var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
            var signInManager = Context.GetOwinContext().Get<ApplicationSignInManager>();
            var session = Context.GetOwinContext().Get<IDocumentSession>();

            //EMAIL CONFIRMATION TURNED OFF UNTIL SERVER ACCESS FIXED
            //var user = new ApplicationUser() { UserName = UserName.Text, Email = Email.Text, PhoneNumber = PhoneNumber.Text };

            var user = new ApplicationUser() { UserName = UserName.Text, Email = Email.Text, IsEmailConfirmed = true, PhoneNumber = PhoneNumber.Text };
            session.Store(user);
            IdentityResult result = manager.Create(user, Password.Text);

            if (result.Succeeded)
            {
                session.SaveChanges();

                // 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);

                IdentityMessage msg = new IdentityMessage()
                    {
                        Destination = user.Email,
                        Subject = "QuoteForm Email Confirmation",
                        Body = "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>."
                    };

                //EMAIL CONFIRMATION TURNED OFF UNTIL SERVER ACCESS FIXED
                //manager.EmailService.SendAsync(msg);

                Response.Redirect("~/");
            }
            else
            {
                ErrorMessage.Text = result.Errors.FirstOrDefault();
            }
        }
        private void CreateAndLoginUser()
        {
            if (!IsValid)
            {
                return;
            }
            var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>();
            var signInManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();
            var user = new ApplicationUser() { UserName = email.Text, Email = email.Text };
            IdentityResult result = manager.Create(user);
            if (result.Succeeded)
            {
                var loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo();
                if (loginInfo == null)
                {
                    RedirectOnFail();
                    return;
                }
                result = manager.AddLogin(user.Id, loginInfo.Login);
                if (result.Succeeded)
                {
                    signInManager.SignIn(user, isPersistent: false, rememberBrowser: 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(Request.QueryString["ReturnUrl"], Response);
                    return;
                }
            }
            AddErrors(result);
        }