我正在构建dnn
模块,它允许登录用户以其他用户身份登录。
但我在这里有一些有线问题。
这是我如何注销当前用户并登录为另一个用户:以其他用户身份登录时丢失会话/ cookie
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);
}
而在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;
}
...
我也试图做与会议相同,但没有任何作品,我不明白为什么?
我的问题是否足够清楚? – 1110
你是否已经逐步完成代码并确保UserInfo和UserId在将它们分配给session/cookie时包含有效值? – CheckRaise
您是否使用过Fiddler等工具来验证cookie是否正在从服务器发送到浏览器中? – user1429080