How to: Customize the Authentication Cookie from the WCF Authentication Service
This topic shows how to customize the authentication cookie (ticket) for the ASP.NET authentication service when it is used as a Windows Communication Foundation (WCF) service. You customize the authentication cookie when you want to store user-specific data in the cookie during authentication.
In general, storing user-specific data in ASP.NET profile properties is a better option than storing data in the cookie. Profile property data is not bound to one computer or limited to the lifetime of the cookie. In addition, you can store the user data more securely in profile properties. Customizing the content of the cookie is useful if you have a small amount of non-sensitive data and do not want to use the ASP.NET profile feature.
The authentication service raises the CreatingCookie event after the user credentials have been validated and before the authentication cookie has been set. You can customize the cookie by creating an event handler for CreatingCookie and managing the authentication cookie yourself. You can access the user name, password, and custom credentials through the CreatingCookieEventArgs object that is passed to the event handler.
You use the WCF implementation of the authentication service when you want to log a user in from a client application that can send and consume a SOAP 1.1 message, such as a Java application.
To customize the authentication cookie
In the Global.asax file of the Web application, create an event handler for the CreatingCookie event.
In the handler, add information to the cookie's CustomCredential property.
Sub AuthenticationService_CreatingCookie(ByVal sender As Object, _ ByVal e As System.Web.ApplicationServices.CreatingCookieEventArgs) Dim ticket As FormsAuthenticationTicket = New _ FormsAuthenticationTicket _ (1, _ e.Username, _ DateTime.Now, _ DateTime.Now.AddMinutes(30), _ e.IsPersistent, _ e.CustomCredential, _ FormsAuthentication.FormsCookiePath) Dim encryptedTicket As String = FormsAuthentication.Encrypt(ticket) Dim cookie As HttpCookie = New _ HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket) cookie.Expires = DateTime.Now.AddMinutes(30) HttpContext.Current.Response.Cookies.Add(cookie) e.CookieIsSet = True End Sub
In the Application_Start method of the Global.asax file, bind the event handler for the CreatingCookie event.
The following example shows how to bind a handler to the CreatingCookie event.
Call the authentication service from an application that can consume a SOAP message from a Web service.
You must set up the authentication service on a Web server for the previous examples to work. For more information, see How to: Enable the WCF Authentication Service.
If you are passing sensitive user data such as authentication credentials, always access the authentication service over the secure sockets layer (SSL, by using HTTPS protocol). For information about how to set up SSL, see Configuring Secure Sockets Layer (IIS 6.0 Operations Guide).