用户登录后,Asp.net登录状态没有显示登出

本文关键字:登录 显示 net Asp 用户 状态 | 更新日期: 2023-09-27 18:06:02

我有一个asp.net web应用程序,允许用户登录和注销。当用户登录时(没有问题),它显示欢迎用户名,这很好。但旁边还是写着登录。有没有人可以帮助我,有一些属性,我需要设置登录?谢谢你的帮助。

<asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
                    <AnonymousTemplate>
                        [ <a href="~/Account/Login.aspx" id="HeadLoginStatus" runat="server">Log In</a>
                        ]
                    </AnonymousTemplate>
                    <LoggedInTemplate>
                        <span class="bold">Welcome</span><span class="bold">
                            <asp:LoginName ID="HeadLoginName" runat="server" />
                        </span>! [
                        <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out"
                            LogoutPageUrl="~/" />
                        ]
                    </LoggedInTemplate>
                </asp:LoginView>
protected void LoginUser_Authenticate(object sender, AuthenticateEventArgs e)
    {
       SqlHelper userLogin = new SqlHelper();
        TextBox User = (TextBox) LoginUser.FindControl("UserName");
        TextBox Password = (TextBox) LoginUser.FindControl("Password");
        bool results = userLogin.UserLogin(User.Text, Password.Text);
        e.Authenticated = results;
    }

用户登录后,Asp.net登录状态没有显示登出

您有两个ID为HeadLoginStatus的控件。这有什么原因吗?其次,您是否尝试删除EnableViewState="false"?

编辑

尝试将启动页面更改为根目录下的页面,如果还没有的话。看看这是否有什么不同。

我似乎已经找到了问题……或者解决方案取决于你如何查看。

我将我的启动页面设置为子目录中的页面,而不是在根。我将启动页面更改为根目录中的页面(即登录的地方。Aspx已经存在),现在它可以正常工作了。我不认为它应该是这样的

仍然不确定为什么它不认证,但我找到了一个工作周围的工作当他们点击登出时(我必须将登出更改为登录状态登出),执行以下操作

if (Request.IsAuthenticated)
        {
          FormsAuthentication.SignOut();
          Session.Abandon();
          // clear authentication cookie
          HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
          cookie1.Expires = DateTime.Now.AddYears(-1);
          Response.Cookies.Add(cookie1);
          // clear session cookie (not necessary for your current problem but i would recommend you do it anyway)
          HttpCookie cookie2 = new HttpCookie("ASP.NET_SessionId", "");
          cookie2.Expires = DateTime.Now.AddYears(-1);
          Response.Cookies.Add(cookie2);
         Response.Redirect("~/");
        }   

删除Anynymoustemplate从来没听说过。它不应该在那里。

<AnonymousTemplate>
 [ <a href="~/Account/Login.aspx" id="HeadLoginStatus" runat="server">Log In</a>
 ]
 </AnonymousTemplate>
编辑:对不起,我做错了。由于某些原因,您的匿名模板在登录后仍然显示