- Microsoft Windows Azure Development Cookbook
- Neil Mackenzie
- 799字
- 2021-04-02 18:51:57
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:
- Add a class named
AppFabricCachingExample
to the project. - Add the following assembly references to the project:
Microsoft.ApplicationServer.Caching.Client Microsoft.ApplicationServer.Caching.Core
- Add the following
using
statements to the top of the class file:using Microsoft.ApplicationServer.Caching; using System.Security;
- Add the following private members to the class:
Int32 cachePort = 22233; String hostName; String authenticationToken; DataCache dataCache;
- Add the following constructor to the class:
AppFabricCachingExample(String hostName, String authenticationToken) { this.hostName = hostName; this.authenticationToken = authenticationToken; }
- Add the following method, creating a
SecureString
, to the class: - 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(); }
- Add the following method, inserting an entry to the cache, to the class:
private void PutEntry( String key, String value) { dataCache.Put(key, value); }
- 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; }
- 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.
- PS職場達人煉成記:人人都能學會的Photoshop辦公設計技巧
- Flash CC中文版動畫設計與制作/微課堂學電腦
- Hi!扁平化Photoshop扁平化用戶界面設計教程
- Moldflow 2010完全自學與速查手冊(模流分析·成本控制)
- 穿越Photoshop CC
- Photoshop CC 2017從入門到精通
- After Effects影視特效立體化教程:After Effects 2021(微課版)
- 輕松玩轉3D One AI
- CorelDRAW 2020中文版入門、精通與實戰
- iPad+Procreate室內設計手繪表現技法
- AutoCAD 2016入門與提高(超值版)
- 中文版Illustrator 2020基礎教程
- Photoshop CC入門與提高(超值版)
- ASP.NET Core 3從入門到實戰
- 從零開始:Indesign CC 2019設計基礎+商業設計實戰