简体   繁体   English

在 ASP.NET 网页中使用 FormsAuthentication

[英]Using FormsAuthentication in ASP.NET WebPages

I am setting up FormsAuthentication in my web app for the site Administrator.我正在我的 web 应用程序中为站点管理员设置 FormsAuthentication。 This question related to: Using FormsAuthentication - My question, now, is;这个问题与: Using FormsAuthentication - 我现在的问题是; When 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?当管理员“成为现有用户”时, WebSecurity.CurrentUserId填充了他已成为用户的当前用户 ID,还是仍然包含管理员的当前用户 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?如果是后者,我们如何才能让WebSecurity.CurrentUserId返回他当前模拟的用户的用户 ID?

@{
    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.而WebSecurity.CurrentUserId的output不再是Admin的用户id,而是他刚刚成为的用户的id。

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.例子:如果管理员的用户id是3,用户名:joe@harry.com的用户id是:56,那么用上面的代码,WebSecurity.CurrentUserId的output变成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.可用于登录/注销特定用户,字符串 a username 和 bool 值为 true/false 用于登录/注销。

FormsAuthentication.SetAuthCookie("user", true); will log in the user and the WebSecurity will have its userID.将登录用户,WebSecurity 将拥有其用户 ID。

FormsAuthentication.SetAuthCookie("admin", false); will log out admin and will remove its userID from WebSecurity.将注销 admin 并从 WebSecurity 中删除其用户 ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM