/// <summary>
    /// Sets the password for a user, not typically allowed by MembershipProvider.
    /// </summary>
    /// <param name="user"></param>
    /// <param name="password"></param>
    /// <returns></returns>
    public static bool SetPassword(this MembershipUser user, string password)
    {
        Transaction transaction = new Transaction(System.Data.IsolationLevel.ReadCommitted, "password change");

        try
        {
            int        userId     = user.GetUserId().Id;
            UserEntity userEntity = new UserEntity(userId);
            if (!userEntity.IsNew)
            {
                transaction.Add(userEntity);
                EpicMembershipProvider.SetPassword(userEntity, password, transaction);
                userEntity.Save();

                transaction.Commit();

                return(true);
            }
            else
            {
                return(false);
            }
        }
        catch
        {
            transaction.Rollback();

            return(false);
        }
        finally
        {
            transaction.Dispose();
        }
    }
Esempio n. 2
0
        private User Get(MembershipUser membershipUser)
        {
            var userId = membershipUser.GetUserId();
            var user   = Execute(db => db.Users.Single(x => x.UserId == userId));

            user.MembershipUser = membershipUser;
            return(user);
        }
    /// <summary>
    /// Change the current user's username and update the current authentication cookie
    /// to include the new username.
    /// This process isn't as simple as just updating the user's entity record in the
    /// database.
    /// Since it's the current user, the authentication cookie must be updated with the
    /// new username.
    /// This is done by signing out the user and then reauthenticating the session with
    /// the new username.
    /// </summary>
    /// <param name="user">current logged in user's membership</param>
    /// <param name="newUsername">user's new username</param>
    /// <param name="saver">a delegate that will be invoked to actually save the new
    ///		username to the database; if not provided, the username will simply be
    ///		updated in the current logged-in user's record; this allows the update to
    ///		be performed within a transaction</param>
    /// <returns><code>true</code> if successful, <code>false</code> if the new username
    ///		is not unique</returns>
    public static bool ChangeUsername(this MembershipUser user, string newUsername, SaveUsername saver = null)
    {
        if (saver == null)
        {
            saver = delegate(string username)
            {
                var userEntity = new UserEntity(user.GetUserId().Id)
                {
                    Username = username
                };
                userEntity.Save();
            }
        }
        ;

        if (UserUtils.GetByUsername(newUsername) == null)
        {
            HttpCookie authCookie = HttpContext.Current.Request.Cookies[FormsAuthentication.FormsCookieName];
            if (authCookie != null)
            {
                var authTicket = FormsAuthentication.Decrypt(authCookie.Value);
                var identity   = new FormsIdentity(
                    new FormsAuthenticationTicket(
                        authTicket.Version,
                        newUsername,
                        authTicket.IssueDate,
                        authTicket.Expiration,
                        authTicket.IsPersistent,
                        authTicket.UserData));
                string[] roles = authTicket.UserData.Split(new[] { '|' });

                saver(newUsername);

                HttpContext.Current.User = new GenericPrincipal(identity, roles);

                FormsAuthentication.SignOut();
                HttpContext.Current.Session.Abandon();
                FormsAuthentication.SetAuthCookie(newUsername, authTicket.IsPersistent);

                return(true);
            }
        }

        return(false);
    }
}