Is it possible to change the username with the Membership API

后端 未结 7 1973
独厮守ぢ
独厮守ぢ 2020-12-03 13:38

I am using the default sql membership provider with ASP.NET and I would like to provide a page to change the user\'s username. I believe I am sure I could do this with a cus

相关标签:
7条回答
  • 2020-12-03 13:51

    If you want to do that with the Membership API, it seems the right way would be like this:

    http://omaralzabir.com/how_to_change_user_name_in_asp_net_2_0_membership_provider/

    Basically, you have to do the following (I copied from the above link, for sake of completeness):

    1. Create a new user using the new email address
    2. Get the password of the old account and set it to the new account. If you can’t get the old password via Membership provider, then ask user.
    3. Create a new profile for the new user account
    4. Copy all the properties from the old profile to the new profile object.
    5. Log out user from old account
    6. Auto sign in to the new account so that user does not notice what an incredible thing just happened.
    0 讨论(0)
  • 2020-12-03 13:52

    It's true that the default SQL Membership Provider does not allow username changes. However, there's no intrinsic reason to prevent users from changing their usernames if you have a valid argument, on your site, to allow it. None of the tables in the SQL database have the username as a key, everything is based on the user's ID, so from an implementation perspective it would be fairly easy.

    0 讨论(0)
  • 2020-12-03 13:54

    Since Membershiop API does not allow username modification directly, you can access directly the aspnet_Membership table in your database.

    0 讨论(0)
  • 2020-12-03 14:00

    Scott Mitchell has a great article describing how to handle this situation here: http://www.4guysfromrolla.com/articles/070109-1.aspx

    Important quote from his article:

    Unfortunately, idealism and pragmatism only rarely intersect. In some cases - such as allowing a user to change their username - we have no choice but to work directly with the underlying data store.

    He also shows how to re-authenticate the user after changing their username/email.

    0 讨论(0)
  • 2020-12-03 14:03

    No, the MembershipUser class does not allow to modify the Username property so you cannot do it.

    Practically you should not allow the username to change. If you allow to do so somehow then it will lose its purpose and nature.

    0 讨论(0)
  • 2020-12-03 14:06

    Here's a version that incorporates the Enterprise Libraries DAB and a couple other minor changes. Also, I don't see a point in returning a Boolean, since it's either going succeed or throw an exception.

            public static void ChangeUsername(string oldUsername, string newUsername)
        {
            if (string.IsNullOrWhiteSpace(oldUsername))
            {
                throw new ArgumentNullException("oldUsername cannot be null or empty");
            }
    
            if (string.IsNullOrWhiteSpace(newUsername))
            {
                throw new ArgumentNullException("newUsername cannot be null or empty");
            }
    
            if (oldUsername.Equals(newUsername))
            {
                return;
            }
    
            Database db = DatabaseFactory.CreateDatabase();
            using (DbCommand cmd = db.GetSqlStringCommand("UPDATE dbo.aspnet_Users SET UserName=@NewUsername, LoweredUserName=@LoweredNewUsername WHERE UserName=@OldUsername"))
            {
                db.AddInParameter(cmd, "@OldUsername", DbType.String, oldUsername);
                db.AddInParameter(cmd, "@NewUsername", DbType.String, newUsername);
                db.AddInParameter(cmd, "@LoweredNewUsername", DbType.String, newUsername.ToLower());
    
                db.ExecuteNonQuery(cmd);                
            }
        }
    
    0 讨论(0)
提交回复
热议问题