用户登录asp.net时显示注销

时间:2014-05-16 20:00:49

标签: c# asp.net .net login

我的母版页中有一个登录控件,如下所示:
enter image description here

我可以在用户登录后隐藏我的登录控件,但是我想显示Logout来代替登录控件 我尝试了LoginStatus,登出后仍然会显示登录字样。 我想要的就是在我登录后隐藏登录(我可以这样做)并显示Logout 点击退出后,它就会消失。 任何的想法?
这是我在aspx中的登录控件:

<asp:Login ID="SAP_Login" runat="server" TitleText="" UserNameLabelText="Email:" PasswordLabelText="Password:" Orientation="Horizontal" VisibleWhenLoggedIn="false"
    DisplayRememberMe="true" PasswordRecoveryText="Forgot your password?" PasswordRecoveryUrl="~/ResetPassword.aspx" ValidatorTextStyle-ForeColor="Red"
    RememberMeText="Remember me" RememberMeSet="true" OnAuthenticate="SAP_Login_Authenticate" OnLoggedIn="SAP_Login_LoggedIn">
    <LayoutTemplate>
        <table cellpadding="1" cellspacing="0" style="border-collapse: collapse; background-color: #dddedf; float: right;">
            <tr>
                <td>
                    <table cellpadding="0">
                        <tr>
                            <td style="font-family: Tahoma;">
                                <asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Email:</asp:Label>
                            </td>
                            <td style="font-family: Tahoma;">
                                <asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:
                                    <asp:HyperLink ID="PasswordRecoveryLink" runat="server" NavigateUrl="~/ResetPassword.aspx">Forgot your password?</asp:HyperLink></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                <asp:TextBox ID="UserName" runat="server" Font-Size="Small" Height="12px"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName" ErrorMessage="User Name is required." ForeColor="Red" ToolTip="User Name is required." ValidationGroup="ctl00$SAP_Login">*</asp:RequiredFieldValidator>
                            </td>
                            <td>
                                <asp:TextBox ID="Password" runat="server" Font-Size="Small" Height="12px" TextMode="Password"></asp:TextBox>
                                <asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password" ErrorMessage="Password is required." ForeColor="Red" ToolTip="Password is required." ValidationGroup="ctl00$SAP_Login">*</asp:RequiredFieldValidator>
                            </td>
                        </tr>
                        <tr>
                            <td class="checkbox">
                                <asp:CheckBox ID="RememberMe" runat="server" Checked="True" Text="Remember me" />
                            </td>
                            <td>
                                <asp:Button ID="LoginButton" runat="server" BackColor="#1872AB" BorderStyle="None" CommandName="Login" ForeColor="White" Text="Log In" ValidationGroup="ctl00$SAP_Login" />
                            </td>
                        </tr>
                        <tr>
                            <td colspan="6" style="color: Red;">
                                <asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
                            </td>
                        </tr>
                        <tr>
                            <td colspan="6"></td>
                        </tr>
                    </table>
                </td>
            </tr>
        </table>
    </LayoutTemplate>
    <LoginButtonStyle BackColor="#1872AB" ForeColor="#FFFfff" BorderStyle="None" />
    <CheckBoxStyle CssClass="checkbox" HorizontalAlign="Left" />
    <LabelStyle Font-Names="Tahoma" />
    <TextBoxStyle Height="12px" Font-Size="Small" />
</asp:Login>

这是C#代码Authenticate

protected void SAP_Login_Authenticate(object sender, AuthenticateEventArgs e)
    {
        SqlDataReader LoginInfo = clsBP.BP_Id_Name(SAP_Login.UserName, SAP_Login.Password);
        if (LoginInfo.HasRows)
        {
            while (LoginInfo.Read())
            {
                FormsAuthentication.RedirectFromLoginPage(SAP_Login.UserName, SAP_Login.RememberMeSet);
                Response.Cookies["userinfo"]["cardcode"] = LoginInfo["CardCode"].ToString();
                Response.Cookies["userinfo"]["cardname"] = LoginInfo["CardName"].ToString();
                Response.Cookies["userinfo"].Expires = DateTime.Now.AddYears(20);

            } 

            Response.Redirect("Shopping.aspx"); SAP_Login.RememberMeSet = true;
        }
        else
        { SAP_Login.FailureText = "Invalid username or password"; }
        LoginInfo.Dispose();
    }

0 个答案:

没有答案