Programming Microsoft ASP.NET 4 - Dino Esposito [415]
After you call SignOut, you might want to redirect the application to another page. The FormsAuthentication class has a method—RedirectToLoginPage—that provides the described functionality and transfers the user to a given page using Response.Redirect.
Let’s now take a look at the methods of the FormsAuthentication class and the configurable parameters you find in the web.config file. After this, I’ll move on to introduce the membership API and role management.
The FormsAuthentication Class
The FormsAuthentication class supplies some static methods you can use to manipulate authentication tickets and execute basic authentication operations. You typically use the RedirectFromLoginPage method to redirect an authenticated user back to the originally requested URL; likewise, you call SignOut to remove the authentication ticket for the current user. Other methods and properties are for manipulating and renewing the ticket and the associated cookie.
Properties of the FormsAuthentication Class
Table 19-4. Properties of the FormsAuthentication Class Property Description CookieDomain Returns the domain set for the authentication ticket. This property is equal to the value of the domain attribute in the CookieMode Indicates whether Forms authentication is implemented with or without cookies. CookiesSupported Returns true if the current request supports cookies. DefaultUrl Returns the URL for the page to return after a request has been successfully authenticated. It matches the defaultUrl attribute in the EnableCrossAppRedirects Indicates whether redirects can span different Web applications. FormsCookieName Returns the configured cookie name used for the current application. The default name is .ASPXAUTH. FormsCookiePath Returns the configured cookie path used for the current application. The default is the root path (/). LoginUrl Returns the configured or default URL for the login page. It matches the loginUrl attribute in the RequireSSL Indicates whether a cookie must be transmitted using only HTTPS. SlidingExpiration Indicates whether sliding expiration is enabled. Most of the properties are initialized with the values read from the Methods of the FormsAuthentication Class Table 19-5. Methods of the FormsAuthentication Class Method Description Authenticate Attempts to validate the supplied credentials against those contained in the configured Decrypt Given a valid authentication ticket, it returns an instance of the FormsAuthenticationTicket class. Encrypt Produces a string containing the printable representation of an authentication ticket. The string contains, encoded to URL-compliant characters, the user’s credentials optionally hashed and encrypted. GetAuthCookie Creates an authentication ticket for a given user name. GetRedirectUrl Returns the redirect
Table 19-4 lists the properties of the FormsAuthentication class. As you can see, many of them deal with cookie naming and usage and expose the content of configuration attributes in the
Table 19-5 details the methods supported by the FormsAuthentication class. All the methods listed in the table are static.