Using FormsAuthentication in ASP.NET WebPages
I am setting up FormsAuthentication in my web app for the site Administrator. This question related to: Using FormsAuthentication - My question, now, is; Whe开发者_C百科n the Admin has "become an existing user", does the WebSecurity.CurrentUserId
become filled with the Current User Id of the user he has become, or is it still containing the Administrator's Current User Id?
And if the latter, how can we make it so that WebSecurity.CurrentUserId
returns the User Id of the user he is currently impersonating?
@{
if(Roles.IsUserInRole("Administrator"))
{
FormsAuthentication.SetAuthCookie(
"joe@harry.com",
false
);
Response.Redirect("~/Account/Page.cshtml");
}
}
<!DOCTYPE html>
<html lang="en">
<body>
<p>You are now no longer an "admin", but user: @WebSecurity.CurrentUserId</p>
</body>
</html>
The above code works. And the output of WebSecurity.CurrentUserId no longer is the Admin's user id, but the Id of the user he has just become.
Example: If the admin's user id is 3, and the user id of username: joe@harry.com is: 56, then with the above code, the output of WebSecurity.CurrentUserId becomes 56.
FormsAuthentication.SetAuthCookie(string, bool);
can be used to login/logout a particular user with the string a username and bool value as true/false for log in/out.
FormsAuthentication.SetAuthCookie("user", true);
will log in the user and the WebSecurity will have its userID.
FormsAuthentication.SetAuthCookie("admin", false);
will log out admin and will remove its userID from WebSecurity.
精彩评论