Share via


Authenticating 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.

An example of when you might want to use a temporary token for authentication is when you want to improve the performance of your application by passing the token instead of frequently authenticating a user during a single session.

Typical Goals

In this scenario, the goal is to use the token as an alternative to using credentials.

Solution

Call the GetIdentity method of the security cache object, specifying the token that was created when the identity was originally cached.

QuickStart

For an extended example of how to use the GetIdentity method to retrieve a user identity, see Walkthrough: Authenticate a User Using a Token.

Using GetIdentity

The following code shows how to use the GetIdentity method.

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

// Cache the identity. 
IToken token = secCache.SaveIdentity(new GenericIdentity("Username"));

// Retrieves the identity previously saved by using the corresponding token.
IIdentity savedIdentity = secCache.GetIdentity(token);
'Usage
Dim secCache As ISecurityCacheProvider = SecurityCacheFactory.GetSecurityCacheProvider("Caching Store Provider")

' Cache the identity.
Dim token As IToken = secCache.SaveIdentity(New GenericIdentity("Username"))

' Retrieves the identity previously saved by using the corresponding token.
Dim savedIdentity As IIdentity = secCache.GetIdentity(token)

Usage Notes

Alternately, you can use the GetPrincipal or GetProfile methods to retrieve a user principal or a user profile.

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.