【发布时间】:2016-07-12 22:59:07
【问题描述】:
我有一个 asp.net 网站,它需要登录并保存到会话内的一个类中。当会话到期时(或当我清除它时),我需要用户再次登录,但最好不要返回主登录页面(因为用户会丢失他们之前输入的所有内容)。
在母版页 OnInit 上,我检查会话是否为空,如果是,则它们已注销。我在我的母版页上创建了一个 jquery 模式对话框,它接收登录信息并在有效登录时重新创建会话。
这可行,但是,对话框会在他们登录后第二次打开并且对话框关闭(此时会话不为空)。
我尝试在登录过程中设置不同的会话值,这样它就不会打开模式对话框,但它看不到它并无论如何都会打开它(并且看到登录会话已经为空,即使它不是不。
退出对话框,一切正常。
谁能告诉我哪里出了问题或者如何防止对话框打开两次?
母版页:
<script type="text/javascript">
function showloginpopup() {
$("#popuplogin").dialog({
resizable: false,
width: 320,
height: 310,
modal: true,
// dialogClass: "no-close", // adding this would remove the x button
appendTo: "form",
buttons: {
}
});
};
</script>
<asp:UpdatePanel ID="upLogin" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div id="popuplogin" style="display: none;" class="ticker" title="Login">
<asp:Panel ID="pnlWorkArea" runat="server" CssClass="login_workarea_panel" DefaultButton="btnLogin">
<%-- textboxes and btnLogin for logging in --%>
</asp:Panel>
</div>
</ContentTemplate>
</asp:UpdatePanel>
btnLogin 读取文本框并使用用户信息填充我的会话。
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if (Classes.LoggedInUser.Current.loggedInUser == null) // This is returning null on 2nd time even though after first time it is set
{
ScriptManager.RegisterStartupScript(this, GetType(), "Show Login Popup", "showloginpopup();", true);
loadData(); // Populate some buttons based on permissions from login
}
}
谢谢!
【问题讨论】:
-
我的建议是,您可以使用另一个会话或查询字符串,同时将弹出窗口作为标志打开,一旦打开,将其标记为 true,因此下次为 true 时,将不会显示该弹出窗口,(请注意,这不是一个好方法,因为您正在创建新会话,它可能仅为此目的占用大量内存,但我建议不考虑内存使用问题。:))
-
我确实尝试过这个 if (Classes.LoggedInUser.Current.loggedInUser == null && Session["mylogin"] == null) - 然后设置会话,然后然后在登录期间重置为空 - 没有区别:|就好像对话框已经决定在会话设置之前第二次打开一样
-
不是问题,你有没有调试并检查你在“loggedInUser”中登录成功后得到了什么,如果我没有错,那么“loggedInUser”是某个类的常量(它可能是系统安全类,不),但我坚持用 Session["MyLogin"].Tostring() == NULL && Session["MyLogin"].Tostring() == "" 直接检查(用空白检查总是好的。)
-
好的,正在逐步执行.. Current.loggedInUser 为空,正在运行 scriptmanager 行。然后它立即再次进入 OnInit,看到 Current.loggedInUser 为空并再次运行 scriptmanager 行(实际上打开了对话框)然后对话框打开我输入凭据,btnLogin 导致回发(它将重新填充 Current.loggedInUser)所以我们再次获取 OnInit,它看到 Current.loggedInUser 为 null(因为它直到 OnInit 之后才设置) - 所以再次运行 scriptmanager 行,在 scriptmanager 行停止第二次运行后设置会话 [继续]
-
这会阻止对话框打开。在 btnLogin 上设置它不起作用,因为 OnInit 在 btnLogin_Click 之前运行 - 所以因为 btnLogin_Click 修复了我的用户会话并设置了这个新会话,所以它们在 OnInit 时都为空,这意味着会话永远不会设置并且对话框出现两次(对话框打开和用户会话已设置,因此退出对话框仍然填充了用户会话)。呃。