Walkthrough: Authenticate a User Using a Token

Retired Content

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The latest Enterprise Library information can be found at the Enterprise Library site.

This walkthrough demonstrates how to use the Security Application Block to authenticate a user using a token.

To reproduce the demonstration

  1. Authenticate the user. The following code demonstrates how to authenticate a user using the System.Web.Security.Membership class.

    IIdentity identity = null;
    if (Membership.ValidateUser("username", "password"))
    {
      identity = new GenericIdentity("username", Membership.Provider.Name);
    }
    
    'Usage
    Dim identity As IIdentity = Nothing
    If (Membership.ValidateUser("username", "password")) Then
      identity = New GenericIdentity("username", Membership.Provider.Name)
    End If
    
  2. Create the security cache by adding the following code.

    ISecurityCacheProvider secCache = SecurityCacheFactory.GetSecurityCacheProvider("Caching Store Provider");
    
    'Usage
    Dim secCache As ISecurityCacheProvider = SecurityCacheFactory.GetSecurityCacheProvider("Caching Store Provider") 
    
  3. Call the SaveIdentity method on the security cache object by adding the following code.

    IToken token = null; 
    if (identity != null)
    {
      token = secCache.SaveIdentity(identity);
    }
    
    'Usage
    Dim token As IToken = Nothing
    If (Not identity Is Nothing) Then
      token = secCache.SaveIdentity(identity)
    End If
    
  4. Call the GetIdentity method on the security cache object by adding the following code.

    IIdentity savedIdentity = secCache.GetIdentity(token);
    
    'Usage
    Dim savedIdentity As IIdentity = secCache.GetIdentity(token)
    

This returns the identity associated with the token. If the token has been expired, or is not valid, GetIdentity returns null (C#) or Nothing (Visual Basic).