Hi phonghue,
To get LoggedInTemplate work add authentication section in web config inside the system.web section.
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<authentication mode="Forms">
<forms loginUrl="Login.aspx" defaultUrl="Home.aspx"></forms>
</authentication>
</system.web>
Then instead of Response.Redirect use FormsAuthentication.RedirectFromLoginPage method.
Check below code.
HTML
Master Page
<div class="sitecontainer">
<div id="MainNavWrapper">
<asp:HyperLink ID="HyperLink1" NavigateUrl="../Faculty.pdf" Text="User's Guide" runat="server"
Target="_blank" Visible="False"></asp:HyperLink>
<asp:LoginView ID="LoginView1" runat="server">
<AnonymousTemplate>
<asp:Label ID="Label2" runat="server" Text="Log in as: "></asp:Label>
| <a href="~/default.aspx" id="HeadLoginStatus" runat="server" color="#87cfe6">Log Out
</a>|
</AnonymousTemplate>
<LoggedInTemplate>
| Logged in as:
<asp:LoginName ID="LoginName1" runat="server" />
|
<asp:Label ID="DividerLabel4" runat="server" Visible="False"> | </asp:Label>
<asp:LoginStatus ID="LoginStatus1" runat="server" LogoutAction="RedirectToLoginPage" />
|
</LoggedInTemplate>
</asp:LoginView>
</div>
</div>
<hr />
<div>
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
Login Page
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:Login ID="Login1" runat="server" DisplayRememberMe="true" Height="162px" Width="377px"
OnAuthenticate="LoginButton_Click">
<LayoutTemplate>
<table cellpadding="1" cellspacing="0" style="border-collapse: collapse;">
<tr>
<td>
<table cellpadding="0" style="height: 162px; width: 377px;">
<tr>
<td align="center" colspan="2">
Log In
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">User Name:</asp:Label>
</td>
<td>
<asp:TextBox ID="UserName" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
ErrorMessage="User Name is required." ToolTip="User Name is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td align="right">
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:</asp:Label>
</td>
<td>
<asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
ErrorMessage="Password is required." ToolTip="Password is required." ValidationGroup="Login1">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td colspan="2">
<asp:CheckBox ID="RememberMe" runat="server" Text="Remember me next time." />
</td>
</tr>
<tr>
<td align="center" colspan="2" style="color: Red;">
<asp:Literal ID="FailureText" runat="server" EnableViewState="False"></asp:Literal>
</td>
</tr>
<tr>
<td align="right" colspan="2">
<asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In" ValidationGroup="Login1" />
</td>
</tr>
</table>
</td>
</tr>
</table>
</LayoutTemplate>
</asp:Login>
</asp:Content>
Home Page
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
</asp:Content>
Code
Login
C#
public bool AuthenticateUser(string LDAPStr, string Username, string Password)
{
try
{
// Validate and return true or false.
if (!string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(Password))
{
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
protected void LoginButton_Click(object sender, EventArgs e)
{
if (AuthenticateUser("LDAP://acad.fullerton.edu/DC=acad,DC=fullerton,DC=edu", Login1.UserName, Login1.Password))
{
// Do your checking.
System.Web.Security.FormsAuthentication.RedirectFromLoginPage(Login1.UserName, false);
}
else
{
Response.Redirect("~/Login.aspx");
}
}
VB.Net
Public Function AuthenticateUser(ByVal LDAPStr As String, ByVal Username As String, ByVal Password As String) As Boolean
Try
' Validate and return true or false.
If Not String.IsNullOrEmpty(Username) AndAlso Not String.IsNullOrEmpty(Password) Then
Return True
Else
Return False
End If
Catch
Return False
End Try
End Function
Protected Sub LoginButton_Click(ByVal sender As Object, ByVal e As EventArgs)
' Do your checking.
If AuthenticateUser("LDAP://acad.fullerton.edu/DC=acad,DC=fullerton,DC=edu", Login1.UserName, Login1.Password) Then
System.Web.Security.FormsAuthentication.RedirectFromLoginPage(Login1.UserName, False)
Else
Response.Redirect("~/Login.aspx")
End If
End Sub
Home
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!User.Identity.IsAuthenticated)
{
FormsAuthentication.RedirectToLoginPage();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not User.Identity.IsAuthenticated Then
FormsAuthentication.RedirectToLoginPage()
End If
End Sub
Screenshot