Login View Control Example
In this example, I will explain about how Login view control shows data when user is Login using forms authentication. In login.aspx when username and password are 'deva' then it will store the username to the browser and redirect to Welcome.aspx file. It will show the LoggedInTemplate content if authentication user attempt to view the content Login view control. IF you access Welcome.aspx without login then welcome.aspx page will show AnonymousTemplate content.
Login View Control Example
It contains two templates LoggedInTemplate and AnonymousTemplate. In LoggedInTemplate content will be shown when asp.net knows that user is logged in and AnonymousTemplate content will be shown if the user is not logged in.
In this example, i will expalain about how Login view control shows data when user is Login using forms authentication. In login.aspx when username and password are deva then it will store the username to the browser and redirect to Welcome.aspx file which will show the LoggedInTemplate content if authentication user attempt to view the content Login view control. IF you access Welcome.aspx without login then welcome.aspx page will show AnonymousTemplate content.
The following code of web.config file contains default forms authentication
&lT;?xml version="1.0"?>
&lT;!--
-->
&lT;configuration>
&lT;connectionStrings>
&lT;add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
&lT;/connectionStrings>
&lT;system.web>
&lT;compilation debug="true" targetFramework="4.0"/>
&lT;authentication mode="Forms">
&lT;forms loginUrl="~/Account/Login.aspx" timeout="2880"/>
&lT;/authentication>
&lT;authorization>
&lT;allow users="*" />
&lT;/authorization>
&lT;membership>
&lT;providers>
&lT;clear/>
&lT;add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
&lT;/providers>
&lT;/membership>
&lT;profile>
&lT;providers>
&lT;clear/>
&lT;add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
&lT;/providers>
&lT;/profile>
&lT;roleManager enabled="true">
&lT;providers>
&lT;clear/>
&lT;add connectionStringName="ApplicationServices" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider"/>
&lT;add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider"/>
&lT;/providers>
&lT;/roleManager>
&lT;/system.web>
&lT;system.webServer>
&lT;modules runAllManagedModulesForAllRequests="true"/>
&lT;/system.webServer>
&lT;/configuration>
In login.aspx file contain the following code:
&lT;%@ Page Title="Log In" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Login.aspx.cs" Inherits="Account_Login" %>
&lT;asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
&lT;/asp:Content>
&lT;asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
&lT;h2>
Log In
&lT;/h2>
&lT;p>
Please enter your username and password.
&lT;asp:HyperLink ID="RegisterHyperLink" runat="server" EnableViewState="false">Register&lT;/asp:HyperLink> if you don't have an account.
&lT;/p>
&lT;asp:Login ID="LoginUser" runat="server" EnableViewState="false" RenderOuterTable="false">
&lT;LayoutTemplate>
&lT;span class="failureNotification">
&lT;asp:Literal ID="FailureText" runat="server">&lT;/asp:Literal>
&lT;/span>
&lT;asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification"
ValidationGroup="LoginUserValidationGroup"/>
&lT;div class="accountInfo">
&lT;fieldset class="login">
&lT;legend>Account Information&lT;/legend>
&lT;p>
&lT;asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Username:&lT;/asp:Label>
&lT;asp:TextBox ID="UserName" runat="server" CssClass="textEntry">&lT;/asp:TextBox>
&lT;asp:RequiredFieldValidator ID="UserNameRequired" runat="server" ControlToValidate="UserName"
CssClass="failureNotification" ErrorMessage="User Name is required." ToolTip="User Name is required."
ValidationGroup="LoginUserValidationGroup">*&lT;/asp:RequiredFieldValidator>
&lT;/p>
&lT;p>
&lT;asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Password:&lT;/asp:Label>
&lT;asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password">&lT;/asp:TextBox>
&lT;asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
CssClass="failureNotification" ErrorMessage="Password is required." ToolTip="Password is required."
ValidationGroup="LoginUserValidationGroup">*&lT;/asp:RequiredFieldValidator>
&lT;/p>
&lT;p>
&lT;asp:CheckBox ID="RememberMe" runat="server"/>
&lT;asp:Label ID="RememberMeLabel" runat="server" AssociatedControlID="RememberMe" CssClass="inline">Keep me logged in&lT;/asp:Label>
&lT;/p>
&lT;/fieldset>
&lT;p class="submitButton">
&lT;asp:Button ID="LoginButton" runat="server" CommandName="Login" Text="Log In"
ValidationGroup="LoginUserValidationGroup" onclick="LoginButton_Click"/>
&lT;/p>
&lT;/div>
&lT;/LayoutTemplate>
&lT;/asp:Login>
&lT;/asp:Content>
In Login.aspx.cs file contain the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
public partial class Account_Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
}
protected void LoginButton_Click(object sender, EventArgs e)
{
//checking the condition for Login control username and password are 'deva'
if (LoginUser.UserName == "deva" && LoginUser.Password == "deva")
{
//Redirect authenticated username to be stored in browser
FormsAuthentication.RedirectFromLoginPage(LoginUser.UserName, false);
//Redirecting to Welcome.aspx
Response.Redirect("Welcome.aspx");
}
else
{
//Redirecting to Welcome.aspx
Response.Redirect("Welcome.aspx");
}
}
}
create a welcome.aspx page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Welcome.aspx.cs" Inherits="Welcome" %>
<!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 runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>LoginView Example</h1>
<asp:LoginStatus ID="LoginStatus1" runat="server" />
<br /><br />
<asp:LoginName ID="LoginName2" runat="server" FormatString="Hi {0}!" Font-Size="XX-Large" ForeColor="DeepPink" />
<br />
<asp:LoginView ID="LoginView1" runat="server">
<LoggedInTemplate>
<h2>Welcome Authenticated User</h2>
</LoggedInTemplate>
<AnonymousTemplate>
<h2>Welcome Anonymous user.</h2>
</AnonymousTemplate>
</asp:LoginView>
</div>
</form>
</body>
</html>
I had attached sample source code for reference.