Exemplo n.º 1
0
 protected void CreateUser_Click(object sender, EventArgs e)
 {
     string userName = UserName.Text;
     try
     {
         User u = new User(userName) { UserName = userName };
         IdentityConfig.Users.Create(u);
         IdentityConfig.Secrets.Create(new UserSecret(userName, Password.Text));
         IdentityConfig.Logins.Add(new UserLogin(u.Id, IdentityConfig.LocalLoginProvider, userName));
         Context.SignIn(u.Id, new Claim[] { }, isPersistent: false);
         IdentityConfig.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
     }
     catch (DbEntityValidationException ex)
     {
         ErrorMessage.Text = "User name already exists:" + ex.EntityValidationErrors.First().ValidationErrors.First().ErrorMessage;
     }
 }
        private async Task CreateAndLoginUserAsync()
        {
            ClaimsIdentity id = await Context.GetExternalIdentity();
            if (id == null)
            {
                ModelState.AddModelError(String.Empty, "There was an error processing this request.");
                return;
            }

            try
            {
                var user = new User(userName.Text);
                if (IdentityConfig.Users.Create(user))
                {
                    IdentityConfig.Logins.Add(new UserLogin(user.Id, ProviderName, ProviderAccountKey));
                    Context.SignIn(user.Id, id.Claims, isPersistent: false);
                    IdentityConfig.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                }
            }
            catch (DbEntityValidationException e)
            {
                ModelState.AddModelError("", e.EntityValidationErrors.First().ValidationErrors.First().ErrorMessage);
            }
        }