protected void CreateUser_Click(object sender, EventArgs e)
        {
            string          firstName = FirstName.Text;
            string          lastName  = LastName.Text;
            string          userName  = UserName.Text;
            string          email     = Email.Text;
            var             manager   = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext()));
            ApplicationUser u         = new ApplicationUser()
            {
                FirstName = firstName,
                LastName  = lastName,
                UserName  = userName,
                Email     = email
            };
            IdentityResult result = manager.Users.CreateLocalUser(u, Password.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();
            }
        }
 protected void LogIn(object sender, EventArgs e)
 {
     if (IsValid)
     {
         // Validate the user password
         IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())).Authentication;
         IdentityResult         result  = manager.CheckPasswordAndSignIn(Context.GetOwinContext().Authentication, UserName.Text, Password.Text, RememberMe.Checked);
         if (result.Success)
         {
             OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
         }
         else
         {
             FailureText.Text     = result.Errors.FirstOrDefault();
             ErrorMessage.Visible = true;
         }
     }
 }
        private void CreateAndLoginUser()
        {
            if (!IsValid)
            {
                return;
            }
            var user = new ApplicationUser();
            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;
            }
        }
Esempio n. 4
0
        private async void CreateUser(string userName, string password, string firstName, string lastName, string email)
        {
            var manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext()));

            var role = await manager.Roles.FindRoleByNameAsync("Registered user");

            if (role == null)
            {
                ErrorMessage.Text = "Role \"Registered user\" missing.";
                return;
            }

            ApplicationUser user = new ApplicationUser()
            {
                UserName  = userName,
                FirstName = firstName,
                LastName  = lastName,
                Email     = email
            };

            IdentityResult result = manager.Users.CreateLocalUser(user, password);

            if (result.Success)
            {
                IdentityResult addToRoleResult = await manager.Roles.AddUserToRoleAsync(user.Id, role.Id);

                if (addToRoleResult.Success)
                {
                    manager.Authentication.SignIn(Context.GetOwinContext().Authentication, user.Id, isPersistent: false);
                    OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                }
                else
                {
                    ErrorMessage.Text = addToRoleResult.Errors.FirstOrDefault();
                }
            }
            else
            {
                ErrorMessage.Text = result.Errors.FirstOrDefault();
            }
        }
 protected void Page_Load()
 {
     // Process the result from an auth provider in the request
     ProviderName = OpenAuthProviders.GetProviderNameFromRequest(Request);
     if (String.IsNullOrEmpty(ProviderName))
     {
         Response.Redirect("~/Account/Login");
     }
     if (!IsPostBack)
     {
         IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore(new ApplicationDbContext())).Authentication;
         var            auth            = Context.GetOwinContext().Authentication;
         ClaimsIdentity id     = manager.GetExternalIdentity(auth);
         IdentityResult result = manager.SignInExternalIdentity(auth, id);
         if (result.Success)
         {
             OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
         }
         else if (User.Identity.IsAuthenticated)
         {
             result = manager.LinkExternalIdentity(id, User.Identity.GetUserId());
             if (result.Success)
             {
                 OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
             }
             else
             {
                 AddErrors(result);
                 return;
             }
         }
         else
         {
             userName.Text = id.Name;
         }
     }
 }