以其他用户身份登录时丢失会话/ cookie

111*_*110 3 c# asp.net dotnetnuke dotnetnuke-module

我正在构建dnn模块,允许登录用户以其他用户身份登录.
但我这里有一些有线问题.
这是我注销当前用户并以另一个用户身份登录的方式:

UserInfo userInfo = UserController.GetUserById(portalId, userId);
if (userInfo != null)
{                
    DataCache.ClearUserCache(this.PortalSettings.PortalId, Context.User.Identity.Name);

    if (Session["super_userId"] == null)
    {
        Session["super_userId"] = this.UserId;
        Session["super_username"] = this.UserInfo.Username;
    }

    HttpCookie impersonatorCookie = new HttpCookie("cookieName");
    impersonatorCookie.Expires = DateTime.Now.AddHours(1);
    Response.Cookies.Add(impersonatorCookie);

    Response.Cookies["cookieName"]["super_userId"] = this.UserId.ToString();
    Response.Cookies["cookieName"]["super_username"] = this.UserInfo.Username;

    PortalSecurity objPortalSecurity = new PortalSecurity();
    objPortalSecurity.SignOut();

    UserController.UserLogin(portalId, userInfo, this.PortalSettings.PortalName, Request.UserHostAddress, false);

    Response.Redirect(Request.RawUrl, true);
}
Run Code Online (Sandbox Code Playgroud)

PageLoad()我尝试从这个cookie读取值但它没有读取任何东西:

try
{
    string super_userId = Request.Cookies["cookieName"]["super_userId"];
    string super_username = Request.Cookies["cookieName"]["super_username"];

    if (!String.IsNullOrEmpty(super_userId))
    {
        this.Visible = true;
        this.lblSuperUsername.Text = Session["super_username"].ToString();
        this.txtPassword.Enabled = true;
        this.btnBackToMyAccount.Enabled = true;
    }
...
Run Code Online (Sandbox Code Playgroud)

我也尝试过对会话做同样的事情,但没有任何作用,我无法理解为什么?

Cod*_*ter 5

因为我觉得在这里,可以有与该被重定向,并且请求设置cookie的问题在这里是说,饼干将不会设置一个重定向时,他们的域名是不是/.

因此,您可以尝试不使用HTTP标头重定向,但显示"登录"页面,而不是包含"主页"链接和元刷新Javascript重定向.

顺便说一句,在cookie中设置UserID并不是真正的方法.如果我将cookie值更改为1怎么办?

  • @ 1110要评论CodeCaster的建议,你应该考虑在cookie中只存储一个会话ID,并在你的本地应用程序变量中保存有关该会话的相关数据,例如UserID等......但这不能回答你的直接问题如何重新直接阅读现有的cookie. (2认同)