Hide registration and login form when user is logged in ASP.NET
I am new at ASP.NET and I am having some difficulties. What I have below is a Registration and Login, both in one page. However, when a user is logged in, I would like the registration and login form to be hided. There is a LOGOUT option when the user is l开发者_如何学Pythonogged in. Any guidance would be great, thank you!
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="Untitled Page" %>
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
End Sub
Protected Sub Login1_Authenticate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs)
End Sub
Protected Sub Login1_Authenticate1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs)
End Sub
Protected Sub Login1_Authenticate2(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.AuthenticateEventArgs)
End Sub
Protected Sub CreateUserWizard1_CreatedUser(ByVal sender As Object, ByVal e As System.EventArgs)
End Sub
</script>
<%-- Add content controls here --%>
<asp:Content ID="Content1" runat="server"
contentplaceholderid="ContentPlaceHolder1">
<table width="640px"><tr>
<td width="360px">
<p>
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server">
<WizardSteps>
<asp:CreateUserWizardStep runat="server" />
<asp:CompleteWizardStep runat="server" />
</WizardSteps>
</asp:CreateUserWizard>
</p>
<br />
<br />
</td>
<td width="270px">
<p>
<asp:Login ID="Login1" runat="server" DestinationPageUrl="~/Home.aspx">
</asp:Login>
<asp:LoginView ID="LoginView1" runat="server">
<AnonymousTemplate>
Please log In
</AnonymousTemplate>
</asp:LoginView>
</p>
<p>
<asp:LoginStatus ID="LoginStatus1" runat="server" />
</p>
</td>
</tr>
</table>
</asp:Content>
You can use User.Identity.IsAuthenticated
to figure out if they're authenticated or not. If they are, you can set the visibility of your control to false.
In your Page_Load method, put something that looks like this:
if (User.Identity.IsAuthenticated)
CreateUserWizard1.Visible = Login1.Visible = False
I'd suggest LoginView.
<asp:LoginView ID="LoginView1" runat="server">
<LoggedInTemplate>
<asp:LoginStatus ID="LoginStatus1" runat="server" />
<asp:LoginName ID="LoginName1" runat="server" />
</LoggedInTemplate>
<AnonymousTemplate>
<p>
<asp:Login ID="Login1" runat="server">
</asp:Login>
</p>
<p>
<asp:CreateUserWizard ID="CreateUserWizard1" runat="server">
<WizardSteps>
<asp:CreateUserWizardStep runat="server" />
<asp:CompleteWizardStep runat="server" />
</WizardSteps>
</asp:CreateUserWizard>
</p>
</AnonymousTemplate>
</asp:LoginView>
<asp:Login VisibleWhenLoggedIn="True|False" />
Gets or sets a value indicating whether to show the Login control after the user is authenticated.
For Example:
<%@ Page Language="VB" AutoEventWireup="False"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Login Sample</title>
</head>
<body>
<form id="form1" runat="server">
<asp:Login id="Login1" runat="server"
VisibleWhenLoggedIn="false">
</asp:Login>
</form>
</body>
</html>
精彩评论