我正在構建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