---
title: "How can you create and manually set a custom Forms Authentication ticket?"  
description: "How can you create and manually set a custom Forms Authentication ticket?"  
author: "ICSM Computer"  
published: 2025-06-01  
updated: 2025-06-01  
canonical: https://www.mindstick.com/interview/34194/how-can-you-create-and-manually-set-a-custom-forms-authentication-ticket  
category: "c#"  
tags: ["c#", "authentication"]  
reading_time: 3 minutes  

---

# How can you create and manually set a custom Forms Authentication ticket?

To create and manually set a custom **Forms Authentication ticket** in ASP.NET, you basically:

1. Create a `FormsAuthenticationTicket` object with your custom data.
2. Encrypt the ticket.
3. Create a cookie containing the encrypted ticket.
4. Add the cookie to the HTTP response.

### Why create a custom ticket?

- To store extra user info in the ticket (like roles or user ID).
- To control ticket expiration and other properties.
- To avoid relying only on `SetAuthCookie` or `RedirectFromLoginPage`.

### Step-by-step example in C#:

```cs
// 1. Create the ticket
var ticket = new FormsAuthenticationTicket(
    1,                      // Ticket version
    username,               // Username
    DateTime.Now,           // Issue date
    DateTime.Now.AddMinutes(30),  // Expiration date
    isPersistent,           // Persistent cookie?
    "UserRole=Admin;UserID=123", // User data (custom info)
    FormsAuthentication.FormsCookiePath // Cookie path
);

// 2. Encrypt the ticket
string encryptedTicket = FormsAuthentication.Encrypt(ticket);

// 3. Create the cookie with the encrypted ticket
var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)
{
    HttpOnly = true,  // Prevent client-side scripts from accessing the cookie
    Secure = FormsAuthentication.RequireSSL,  // Set secure flag if SSL required
    Path = FormsAuthentication.FormsCookiePath
};

// If the ticket is persistent, set the cookie expiration
if (isPersistent)
{
    authCookie.Expires = ticket.Expiration;
}

// 4. Add the cookie to the response
Response.Cookies.Add(authCookie);
```

### What happens next?

- On subsequent requests, ASP.NET automatically decrypts the cookie and recreates the `FormsIdentity` with your ticket data.
- You can access the custom user data like this:

```cs
var identity = (FormsIdentity)User.Identity;
string userData = identity.Ticket.UserData;
// Parse userData string as needed
```

## Answers

### Answer by ICSM Computer

To create and manually set a custom **Forms Authentication ticket** in ASP.NET, you basically:

1. Create a `FormsAuthenticationTicket` object with your custom data.
2. Encrypt the ticket.
3. Create a cookie containing the encrypted ticket.
4. Add the cookie to the HTTP response.

### Why create a custom ticket?

- To store extra user info in the ticket (like roles or user ID).
- To control ticket expiration and other properties.
- To avoid relying only on `SetAuthCookie` or `RedirectFromLoginPage`.

### Step-by-step example in C#:

```cs
// 1. Create the ticket
var ticket = new FormsAuthenticationTicket(
    1,                      // Ticket version
    username,               // Username
    DateTime.Now,           // Issue date
    DateTime.Now.AddMinutes(30),  // Expiration date
    isPersistent,           // Persistent cookie?
    "UserRole=Admin;UserID=123", // User data (custom info)
    FormsAuthentication.FormsCookiePath // Cookie path
);

// 2. Encrypt the ticket
string encryptedTicket = FormsAuthentication.Encrypt(ticket);

// 3. Create the cookie with the encrypted ticket
var authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)
{
    HttpOnly = true,  // Prevent client-side scripts from accessing the cookie
    Secure = FormsAuthentication.RequireSSL,  // Set secure flag if SSL required
    Path = FormsAuthentication.FormsCookiePath
};

// If the ticket is persistent, set the cookie expiration
if (isPersistent)
{
    authCookie.Expires = ticket.Expiration;
}

// 4. Add the cookie to the response
Response.Cookies.Add(authCookie);
```

### What happens next?

- On subsequent requests, ASP.NET automatically decrypts the cookie and recreates the `FormsIdentity` with your ticket data.
- You can access the custom user data like this:

```cs
var identity = (FormsIdentity)User.Identity;
string userData = identity.Ticket.UserData;
// Parse userData string as needed
```


---

Original Source: https://www.mindstick.com/interview/34194/how-can-you-create-and-manually-set-a-custom-forms-authentication-ticket

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
