Obtaining a Temporary Token for an Authenticated User
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 use the approach described here to save a user principal or a user identity in the security cache and obtain a token that represents the user's authenticated identity.
Typical Goals
In this scenario, the goal is to pass the identity of an authenticated user to a cache and retrieve a token corresponding to the identity.
Solution
Declare member variables to hold a token and a cache. Call the SaveIdentity method of the CachingStoreProvider, passing to it the identity of the authenticated user. This must be an instance of a class that implements the IIdentity interface, such as a WindowsIdentity, GenericIdentity, PassportIdentity, or FormsIdentity, depending on the authentication technique used. The SaveIdentity method stores the identity information and returns a token.
Using SaveIdentity
The following code shows how to use the SaveIdentity method. It assumes that you have resolved the ISecurityCacheProvider type through the Enterprise Library container to obtain an instance of the security cache provider you want to use, and stored the reference in a variable named secCache.
Note
For more information on instantiating objects, see Creating and Referencing Enterprise Library Objects.
// Cache the identity. The SecurityCache will generate and return a token.
IToken token = secCache.SaveIdentity(new GenericIdentity("Username"));
'Usage
' Cache the identity. The SecurityCache will generate and return a token.
Dim token As IToken = secCache.SaveIdentity(New GenericIdentity("Username"))
For information about resolving Enterprise Library objects in your applications, see Creating and Referencing Enterprise Library Objects.
Usage Notes
Alternatively, you can use the SavePrincipal or SaveProfile methods to retrieve a token for a principal (an object that implements the IPrincipal interface) or a profile (such as the ASP.NET Profile object). Each of these objects can be grouped together with the same token.