官术网_书友最值得收藏!

Authenticating with the Windows Azure AppFabric Caching Service

The Windows Azure AppFabric Caching Service provides a hosted data cache along with local caching of that data. It provides a cloud-hosted version of the Windows Server AppFabric Caching Service.

All access to the caching service must be authenticated using a service namespace and an authentication token. These are generated on the Windows Azure Portal. The service namespace is similar to the account name used with the storage services. It forms the base of the service URL used in accessing the caching service.

The Windows Azure Access Control Service (ACS) is used to authenticate requests to the caching service. However, the complexity of this is abstracted by the caching service SDK. A DataCacheSecurity instance is constructed from the authentication token. To reduce the likelihood of the authentication token remaining in memory in an accessible form, the DataCacheSecurity constructor requires that it be passed in as a SecureString rather than a simple String. This DataCacheSecurity instance is then added to a DataCacheFactoryConfiguration instance. This is used to initialize the DataCacheFactory used to create the DataCache object used to interact with the caching service.

In this recipe, we will learn how to authenticate to the Windows Azure AppFabric Caching Service.

Getting ready

This recipe uses the Windows Azure AppFabric SDK. It also requires the creation—on the Windows Azure Portal—of a namespace for the Windows Azure AppFabric Caching Service. We see how to do this in the Creating a namespace for the Windows Azure AppFabric recipe in Chapter 9.

How to do it...

We are going to authenticate against the Windows Azure AppFabric Caching Service and cache an item in the service. We do this as follows:

  1. Add a class named AppFabricCachingExample to the project.
  2. Add the following assembly references to the project:
    Microsoft.ApplicationServer.Caching.Client
    Microsoft.ApplicationServer.Caching.Core
  3. Add the following using statements to the top of the class file:
    using Microsoft.ApplicationServer.Caching;
    using System.Security;
  4. Add the following private members to the class:
    Int32 cachePort = 22233;
    String hostName;
    String authenticationToken;
    DataCache dataCache;
  5. Add the following constructor to the class:
    AppFabricCachingExample(String hostName, String authenticationToken)
    {
       this.hostName = hostName;
       this.authenticationToken = authenticationToken;
    }
  6. Add the following method, creating a SecureString, to the class:
    static private SecureString CreateSecureString(String token)
    {
       SecureString secureString = new SecureString();
       foreach (char c in token)
       {
          secureString.AppendChar(c);
       }
       secureString.MakeReadOnly();
       return secureString;
    }
    
  7. Add the following method, initializing the cache, to the class:
    private void InitializeCache()
    {
       DataCacheSecurity dataCacheSecurity =new DataCacheSecurity(CreateSecureString(authenticationToken), false);
    
       List<DataCacheServerEndpoint> server =new List<DataCacheServerEndpoint>();
       server.Add(new DataCacheServerEndpoint(hostName, cachePort));
    
       DataCacheTransportProperties dataCacheTransportProperties =new DataCacheTransportProperties()
       {
          MaxBufferSize = 10000,
          ReceiveTimeout = TimeSpan.FromSeconds(45)
       };
    
       DataCacheFactoryConfiguration dataCacheFactoryConfiguration= new DataCacheFactoryConfiguration()
       {
          SecurityProperties = dataCacheSecurity,
          Servers = server,
          TransportProperties = dataCacheTransportProperties
       };
    
       DataCacheFactory myCacheFactory =new DataCacheFactory(dataCacheFactoryConfiguration);
       dataCache = myCacheFactory.GetDefaultCache();
    }
  8. Add the following method, inserting an entry to the cache, to the class:
    private void PutEntry( String key, String value)
    {
       dataCache.Put(key, value);
    }
  9. Add the following method, retrieving an entry from the cache, to the class:
    private String GetEntry(String key)
    {
       String playWright = dataCache.Get(key) as String;
       return playWright;
    }
  10. Add the following method, invoking the methods added earlier, to the class:
    public static void UseAppFabricCachingExample()
    {
       String hostName = "{SERVICE_NAMESPACE}.cache.windows.net";
       String authenticationToken = "{AUTHENTICATION_TOKEN}";   
    
       String key = "{KEY}";
       String value = "{VALUE}";
    
       AppFabricCachingExample example =new AppFabricCachingExample();
    
       example.InitializeCache();
       example.PutEntry(key, value);
       String theValue = example.GetEntry(key);
    }

How it works...

In steps 1 through 3, we set up the class. In step 4, we add some private members to hold the caching service endpoint information and the authentication token. We initialize these in the constructor we add in step 5.

In step 6, we add a method that creates a SecureString from a normal String. The authentication token used when working with the caching service SDK must be a SecureString. Typically, this would be initialized in a more secure fashion than from a private member.

In step 7, we first initialize the objects used to configure a DataCacheFactory object. We need to provide the authentication token and the caching service endpoint. We specify a ReceiveTimeout of less than 1 minute to reduce the possibility of an error caused by stale connections. We use the DataCacheFactory to get the DataCache for the default cache for the caching service. Note that in this recipe, we did not configure a local cache.

In step 8, we insert an entry in the cache. Note that we use Put() rather than Add() here, as Add() throws an error if the item is already cached. We retrieve it from the cache in step 9.

In step 10, we add a helper method that invokes each of the methods we added earlier. We must replace {SERVICE_NAMESPACE} and {AUTHENTICATION_TOKEN} with actual values for the caching service namespace and authentication token that we created on the Windows Azure Portal. We can replace {KEY} and {VALUE} with appropriate values.

主站蜘蛛池模板: 屯门区| 交口县| 元朗区| 雷波县| 凤冈县| 钟祥市| 朝阳市| 马公市| 株洲市| 文山县| 河北省| 边坝县| 嘉定区| 塔城市| 进贤县| 张家口市| 顺义区| 富锦市| 荆州市| 巴马| 金乡县| 威信县| 巴楚县| 海原县| 汾阳市| 景宁| 芷江| 鞍山市| 双鸭山市| 开原市| 碌曲县| 宣武区| 彰化市| 曲麻莱县| 织金县| 沭阳县| 文水县| 富宁县| 万年县| 彰武县| 揭阳市|