Hi sulfy,
You can retrieve the details from FormsAuthenticationTicket.
Check this example. Now please take its reference and correct your code.
In the example i have checked with hard coded value. Change the code with your code to validate user.
HTML
Login
<h3>Logon Page</h3>
<table>
<tr>
<td>User Name:</td>
<td><asp:TextBox ID="txtUserName" runat="server"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ControlToValidate="txtUserName" Display="Dynamic" ErrorMessage="*" runat="server" /></td>
</tr>
<tr>
<td>Password:</td>
<td><asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ControlToValidate="txtPassword" Display="Dynamic" ErrorMessage="*" runat="server" /></td>
</tr>
<tr>
<td>Persistent Cookie:</td>
<td><asp:CheckBox ID="chkCookie" runat="server" /></td>
<td></td>
</tr>
</table>
<asp:Button Text="Logon" runat="server" OnClick="OnLogon" />
Default
<div>
User Name: <asp:Label ID="lblName" runat="server" /><br />
Cookie Path: <asp:Label ID="lblCookiePath" runat="server" /><br />
Expire Date: <asp:Label ID="lblExpireDate" runat="server" /><br />
Expired: <asp:Label ID="lblExpired" runat="server" /><br />
Is Persistent: <asp:Label ID="lblIsPersistent" runat="server" /><br />
Issue Date: <asp:Label ID="lblIssueDate" runat="server" /><br />
User Data: <asp:Label ID="lblUserData" runat="server" /><br />
Version: <asp:Label ID="lblVersion" runat="server" /><br />
</div>
Namespaces
C#
using System.Web.Security;
VB.Net
Imports System.Web.Security
Code
C#
Login
protected void OnLogon(object sender, EventArgs e)
{
string userName = txtUserName.Text.Trim();
string password = txtPassword.Text.Trim();
bool isCookie = chkCookie.Checked;
if (userName == "test" && password == "test")
{
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddMinutes(30), isCookie, "custom data");
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
if (isCookie)
{
cookie.Expires = ticket.Expiration;
}
cookie.Path = FormsAuthentication.FormsCookiePath;
Response.Cookies.Add(cookie);
Response.Redirect("Default.aspx", true);
}
else
{
Response.Redirect("Login.aspx", true);
}
}
Default
protected void Page_Load(object sender, EventArgs e)
{
if (this.Page.User.Identity.IsAuthenticated)
{
FormsIdentity identity = (FormsIdentity)User.Identity;
FormsAuthenticationTicket ticket = identity.Ticket;
lblName.Text = ticket.Name;
lblCookiePath.Text = ticket.CookiePath;
lblExpireDate.Text = ticket.Expiration.ToString();
lblExpired.Text = ticket.Expired.ToString();
lblIsPersistent.Text = ticket.IsPersistent.ToString();
lblIssueDate.Text = ticket.IssueDate.ToString();
lblUserData.Text = ticket.UserData;
lblVersion.Text = ticket.Version.ToString();
}
else
{
FormsAuthentication.RedirectToLoginPage();
}
}
VB.Net
Login
Protected Sub OnLogon(ByVal sender As Object, ByVal e As EventArgs)
Dim userName As String = txtUserName.Text.Trim()
Dim password As String = txtPassword.Text.Trim()
Dim isCookie As Boolean = chkCookie.Checked
If userName = "test" AndAlso password = "test" Then
Dim ticket As FormsAuthenticationTicket = New FormsAuthenticationTicket(1, userName, DateTime.Now, DateTime.Now.AddMinutes(30), isCookie, "custom data")
Dim cookie As HttpCookie = New HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket))
If (isCookie) Then
cookie.Expires = ticket.Expiration
End If
cookie.Path = FormsAuthentication.FormsCookiePath
Response.Cookies.Add(cookie)
Response.Redirect("Default.aspx", True)
Else
Response.Redirect("Login.aspx", True)
End If
End Sub
Default
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Me.Page.User.Identity.IsAuthenticated Then
Dim identity As FormsIdentity = CType(User.Identity, FormsIdentity)
Dim ticket As FormsAuthenticationTicket = identity.Ticket
lblName.Text = ticket.Name
lblCookiePath.Text = ticket.CookiePath
lblExpireDate.Text = ticket.Expiration.ToString()
lblExpired.Text = ticket.Expired.ToString()
lblIsPersistent.Text = ticket.IsPersistent.ToString()
lblIssueDate.Text = ticket.IssueDate.ToString()
lblUserData.Text = ticket.UserData
lblVersion.Text = ticket.Version.ToString()
Else
FormsAuthentication.RedirectToLoginPage()
End If
End Sub
Screenshot
![](https://i.imgur.com/29w7KJK.gif)