2012-03-20 59 views

回答

0

如果你想獲得後門訪問,你不需要知道用戶的密碼。只是他們的用戶名

你可以通過只寫FormsAuthentication.SetAuthCookie(username, false)來做到這一點。

只要確保您限制訪問此功能。例如,創建一個名爲「身份驗證代理」的角色。在執行後門訪問的控制器操作方法中,請確保只允許「身份驗證代理」角色的用戶使用該方法。

[HttpPost] 
[Authorize("Authentication Agent")] 
public virtual ActionResult SignInAs(SignInAsForm model) 
{ 
    if (model != null) 
    { 
     MembershipUser member = null; 
     if (!string.IsNullOrWhiteSpace(model.UserName)) 
     { 
      model.UserName = model.UserName.Trim(); 
      member = Membership.GetUser(model.UserName); 
     } 

     if (member == null) 
      ModelState.AddModelError("UserName", string.Format(
       "Username '{0}' could not be found.", model.UserName)); 

     if (ModelState.IsValid) 
     { 
      Session["WasSignedInAs"] = User.Identity.Name; 
      FormsAuthentication.SetAuthCookie(model.UserName, false); 
      TempData["FlashMessage"] = string.Format(
       "Impersonation was successful. You are signed in as {0}.", 
        model.UserName) 
      if (!string.IsNullOrWhiteSpace(model.ReturnUrl)) 
       return Redirect(model.ReturnUrl); 
      return RedirectToAction(FormsAuthentication.DefaultUrl); 
     } 
     return View(model); 
    } 
    return HttpNotFound(); 
} 
+0

謝謝,期待明天再試。 – 2012-03-20 14:20:12

+0

它工作完美,再次感謝! – 2012-03-21 10:08:21

相關問題