2016-03-07 46 views
0

我有一個使用asp.net安全性的MVC 5演示應用程序。在該應用程序中,我有75個以上的用戶帳戶。MVC 5 - 更改演示帳戶上的密碼

提供演示的人離開了,所以我希望能夠重置所有帳戶的所有密碼,而無需將每個帳戶上的電子郵件更改爲我的個人電子郵件,並單獨執行鏈接將發送到我的個人電子郵件。

有沒有一種方法可以輸入用戶名和新密碼並使用內置的IdentityUser功能來重置密碼?

回答

0

是的,在帳戶控制器中,只需進入忘記密碼功能,並在首先用戶搜索電子郵件ID並在該系統向該用戶發送郵件之後稍微更改該代碼。 有剛剛寫下代碼,其中用戶將郵件發送到您的特定的電子郵件ID,然後你可以在您的帳戶鏈接點擊該鏈接並重置密碼

1

假設你的應用程序是在標準MVC5格式,把這個的ViewResult進入帳戶控制:

[AllowAnonymous] 
public async Task<ViewResult> ResetAllPasswords() 
{ 
    // Get a list of all Users 
    List<ApplicationUser> allUsers = await db.Users.ToListAsync(); 
    // NOTE: make sure this password complies with the password requirements set up in Identity.Config 
    string newPassword = "YourNewPassword!"; 
    int passwordChangeSuccess = 0; 
    int countUsers = 0; 
    // Loop through the list of Users 
    foreach (var user in allUsers) 
    { 
     // Get the User 
     ApplicationUser thisUser = await UserManager.FindByNameAsync(user.UserName); 
     // Generate a password reset token 
     string token = await UserManager.GeneratePasswordResetTokenAsync(thisUser.Id); 
     // Change the password, using the reset token 
     IdentityResult result = await UserManager.ResetPasswordAsync(thisUser.Id, token, newPassword); 

     // Record results (extend to taste) 
     if (result.Succeeded) 
     { 
      passwordChangeSuccess++; 
     } 
     countUsers++; 
    } 

    ViewBag.CountUsers = countUsers; 
    ViewBag.PasswordSuccess = passwordChangeSuccess; 

    return View(); 
} 

,並設置了ViewBag.CountUsers和ViewBag.PasswordSuccess檢查結果的新景觀。

然後設置一個ActionLink指向帳戶控制器中的ResetAllPasswords並按下即可。

很明顯,格式可以改變(也許是一個確認的形式,也許有一個輸入字段來指定密碼..),但基本的控制器代碼應該很好。並且請注意,[AllowAnonymous]屬性僅適用於一次性訪問 - 並不是一個好主意,因爲它不僅僅是測試!

這應該將所有用戶重置爲代碼中指定的相同密碼。