示例#1
0
        protected void CreateUser_Click(object sender, EventArgs e)
        {
            string userName = TextBoxUserName.Text;
            var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext()));
            User u = new User(userName);

            IdentityResult result = manager.Users.CreateLocalUser(u, TextBoxPassword.Text);
            if (result.Success)
            {
                manager.Authentication.SignIn(Context.GetOwinContext().Authentication, u.Id, isPersistent: false);
                OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
            }
            else
            {
                ErrorMessage.Text = result.Errors.FirstOrDefault();
            }
        }
 private void CreateAndLoginUser()
 {
     if (!IsValid)
     {
         return;
     }
     var user = new User(userName.Text);
     IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())).Authentication;
     IdentityResult result = manager.CreateAndSignInExternalUser(Context.GetOwinContext().Authentication, user);
     if (result.Success)
     {
         OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
     }
     else
     {
         AddErrors(result);
         return;
     }
 }
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                // Create a local login before signing in the user
                var user = new User(model.UserName);
                var result = await IdentityManager.Users.CreateLocalUserAsync(user, model.Password);
                if (result.Success)
                {
                    await IdentityManager.Authentication.SignInAsync(AuthenticationManager, user.Id, isPersistent: false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    AddErrors(result);
                }
            }

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