How to Create FormAuthentication

This Class is useful for Creating FormAuthentication.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.NetworkInformation;
using System.Management;
using System.Security.Principal;

namespace AIMS_Shared
    public class FormAuthentication
        public FormAuthentication()

        #region ::This Method is use for creating Formauthentication ticket and cookie::
        public void CreateFormAuthenticationTicket(string userid, string UserData, HttpResponse httpResponse)
            // Create a new ticket used for authentication
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
            1, // Ticket version
            userid, // Username to be associated with this ticket
            DateTime.Now, // Date/time issued
            DateTime.Now.AddHours(14), // Date/time to expire
            true, // "true" for a persistent user cookie (could be a checkbox on form)
            UserData, // User-data (the roles from this user record in our database)
// Path cookie is valid for

            // Hash the cookie for transport over the wire
            string hash = FormsAuthentication.Encrypt(ticket);
            HttpCookie cookie = new HttpCookie(
            FormsAuthentication.FormsCookieName, // Name of auth cookie (it's the name specified in web.config)
// Hashed ticket

            cookie.Expires = DateTime.Now.AddHours(14);


        #region ::This function is use for geting user identity information ::
        public string GetIdentity(System.Security.Principal.IIdentity iIdentity)
            string userData = "";
            FormsIdentity id = (FormsIdentity)iIdentity;
            FormsAuthenticationTicket ticket = id.Ticket;
            userData = ticket.UserData;
            return userData;

        public string GetCurrentSystemMACAddress()
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc = mc.GetInstances();
            string MACAddress = String.Empty;
            foreach (ManagementObject mo in moc)
                if (MACAddress == String.Empty) // only return MAC Address from first card
                    if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();

            MACAddress = MACAddress.Replace(":", "");
            return MACAddress;           


        public string GetCookieValue(HttpContext Context)
            string CookieValue = string.Empty;
            // Get the authentication cookie
            string cookieName = FormsAuthentication.FormsCookieName;
            HttpCookie authCookie = Context.Request.Cookies[cookieName];
            // If the cookie can't be found, don't issue the ticket
// if (authCookie == null) return;

            // Get the authentication ticket and rebuild the principal
            // & identity
            FormsAuthenticationTicket authTicket =
            string[] roles = authTicket.UserData.Split(new Char[] { '|' });
            //GenericIdentity userIdentity =
            //  new GenericIdentity(authTicket.Name);
            //GenericPrincipal userPrincipal =
            //  new GenericPrincipal(userIdentity, roles);
            //Context.User = userPrincipal;
            CookieValue = roles.ToString();
            return CookieValue;

You must Add  In your  web.config file.

<authentication mode="Forms">
      <forms name="FormsAuthDB.AspxAuth" loginUrl="UserLogin.aspx" protection="All" path="/"/>