Obtaining a Temporary Token for an Authenticated User

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.

An example of when you might want to obtain a temporary token for an authenticated user is when you want to improve the performance of your application by passing the token instead of frequently authenticating the same user during a single session. You can also use the approach described here to save a user principal or a user identity.

Typical Goals

In this scenario, the goal is to pass the identity of an authenticated to a cache and retrieve a token corresponding to the identity.

Solution

Declare member variables to hold a token and a cache. Authenticate the user and initialize the security cache by calling GetSecurityCacheProvider method of the SecurityCacheFactory. Call the SaveIdentity method of the CachingStoreProvider. This stores the identity information and returns a token.

QuickStart

For an extended example of how to use the SaveIdentity method to obtain a token for an authenticated user, see Walkthrough: Obtain a Temporary Token for an Authenticated User.

Using SaveIdentity

The following code shows how to use the SaveIdentity method.

ISecurityCacheProvider secCache = SecurityCacheFactory.GetSecurityCacheProvider("Caching Store Provider");

// Cache the identity. The SecurityCache will generate and return a token.
IToken token = secCache.SaveIdentity(new GenericIdentity("Username"));
'Usage
Dim secCache As ISecurityCacheProvider = SecurityCacheFactory.GetSecurityCacheProvider("Caching Store Provider")

' Cache the identity. The SecurityCache will generate and return a token.
Dim token As IToken = secCache.SaveIdentity(New GenericIdentity("Username"))

Usage Notes

Alternatively, you can use the SavePrincipal or SaveProfile methods to retrieve a token for a principal or a profile. Each of these objects can be grouped together with the same token.

The code shown above uses a factory class in the Security Application Block to obtain an instance of the security cache provider it requires. As an alternative, you can use the Unity Integration approach to create instances of the objects in the Security Application Block. For more details of the integration of Enterprise Library and the Unity Application Block, see Creating Objects Using the Unity Application Block.