您可以在网站的某个位置放置一个锚点:
@Html.ActionLink("elevate to admin", "SwitchToAdmin", "Home")
然后有一个控制器操作,允许输入管理员凭据:
public ActionResult SwitchToAdmin()
{
// TODO: Adjust the role name that your administrators will have
if (!User.IsInRole(@"DOMAIN\Administrators"))
{
// The user is not currently an admin => popup a Logon box
// so that the administrator could authenticate himself
return new HttpUnauthorizedResult();
}
else
{
// After inputting the correct username and password for the
// admin, we can now redirect to the home action and start performing
// the admin tasks
return RedirectToAction("index", "home");
}
}
还原过程将是相反的。如果用户是管理员允许普通用户输入他的用户名和密码,您可以有一个链接将调用控制器操作,该操作将抛出 401。