2014-02-18 29 views
0

我正在構建一個MVC4應用程序。應用程序將用戶的電子郵件地址用作用戶名。 我有一種情況,用戶可能會更改他們的電子郵件地址,這意味着我需要系統能夠更改他的用戶名。如何更新simplemembership中的用戶名?

是否可以使用MVC4和simplemebership?

所以我想改變現有的使用者名稱到一個新的

感謝

回答

0

SimpleMembership不提供更新從WebSecurity API用戶的方法。你必須使用實體框架來做到這一點。

選項一是使用SimpleSecurity Project,它提供WebSecurity的擴展實現。以下是SimpleSecurity Project中單元測試的一個片段,用於測試更新用戶(用戶名被修改)的情況。

[TestMethod] 
    public void TestUpdateUser() 
    { 
     string username = "jdoe"; 
     string password = "password"; 
     string token = WebSecurity.CreateUserAndAccount(username, password, "[email protected]", false); 
     bool validated = WebSecurity.ValidateUser(username, password); 
     Assert.IsTrue(validated, "Failed validation of user."); 
     //Get user for update 
     var user = WebSecurity.GetUser(username); 
     //Change the user name 
     username = "jdoe2"; 
     user.UserName = username; 
     //Update the user information 
     WebSecurity.UpdateUser(user); 
     //Make sure we can still validate them 
     validated = WebSecurity.ValidateUser(username, password); 
     Assert.IsTrue(validated, "Failed validation of user after update."); 

    } 

第二個選項是探索SimpleSecurity中的源代碼,看看它是如何實現和複製的。你可以get the source code here。您可以單步測試SimpleSecurity.UnitTest