ASP.NET Application Providers相信大家都已經非常熟悉,它為我們開發ASP.NET應用程序提供了方便。
比如在Visual Studio控件工具箱中的Login部分,就是基于Membership provider、Session provider和Profile provider,大大地簡化了我們在開發過程中處理與用戶相關的功能。
(更多Provider的信息請查看http://msdn.microsoft.com/en-us/library/aa479030.aspx)
ASP.NET Provider架構示意圖
ASP.NET Provider的類結構圖
Azure Provider是什么
Azure上的應用程序也是ASP.NET 應用程序,所以它也支持ASP.NET 的Provider模型。不同的是,我們平時用的Provider是基于關系數據庫的(如SQL Server),而“云端”上Provider自然也需要使用“云端”的數據解決方案——Windows Azure Storage。
云平臺是基于分布式計算的。這就意味著,你的同一個應用程序可能會分布在不同的機器上。所以包括Session在內的各種用戶數據不能放在host主機上,必須通過Windows Azure Storage來統一管理。
微軟認為:基于Windows Azure Storage的provider 比傳統SQL provider具有以下優勢:
在保持與SQL provider完全相同的使用方法(不用做任何代碼修改)的基礎上,
- 并行處理更強悍。
- 分頁查詢更方便。
- 計算能力更牛X。
- 運行更穩定。
Azure Provider的原理
在Windows Azure SDK(March 2009 CTP)中,微軟已經提供了可直接使用的Azure版ASP.NET Provider。包括membership, role, profile, session state 4種providers。
打開Azure版ASP.NET Provider項目,非常一目了然。以下的4個文件與4種provider一一對應。
拿TableStorageMembershipProvider來說,它其實是繼承了MembershipProvider類(參照上文的provider類結構圖),override了MembershipProvider類里與數據操作相關的方法,將它們換成使用Windows Azure Table Storage版本的。
public class TableStorageMembershipProvider : MembershipProvider
配置使用Azure Provider:
剛才說了,從傳統的SQL Provder轉換到使用Azure Provider,不用做任何代碼上的改動,只需要更改配置文件就可以了。
下面是MemshipProvider的配置示例。(其他Provider也類似,請參考附件中的代碼)
<membership defaultProvider="TableStorageMembershipProvider" 
userIsOnlineTimeWindow = "20">
<providers>
<clear/>
<add name="TableStorageMembershipProvider" 
type="Microsoft.Samples.ServiceHosting.AspProviders.TableStorageMembershipProvider"
description="Membership provider using table storage"
applicationName="ProviderTest"
tableServiceBaseUri=”your table service endpoint”
allowInsecureRemoteEndpoints=”false”
accountName="youraccountname"
sharedKey="yoursharedkey"
membershipTableName="Membership"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
minRequiredPasswordLength="1"
minRequiredNonalphanumericCharacters="0"
requiresUniqueEmail="true"
passwordFormat="Hashed"
/>
</providers>
</membership>
代碼下載:
使用Azure Provider的示例代碼:AspProvidersDemo.rar
Azure Provider源代碼:AspProviders.rar
StorageClient:StorageClient.rar
______________________分割線____________________________________________________
Azure Provider的數據層面:
|
Provider |
Storage |
|
Membership |
Table storage |
|
Profile |
Table storage for storing management data Blob storage for storing the actual profile data |
|
Session state |
Table storage for storing management data Blob storage for storing the actual session state |
|
Role provider |
Table storage |
Membership Table
|
Column |
Type |
Comment |
|
Partition key |
String |
Concatenation of application name and user name |
|
Row key |
String |
Unused, empty string |
|
User name |
String |
|
|
User id |
Guid |
|
|
Password |
String |
Max 128 characters because of encode limitations |
|
Password format |
String |
.NET regular expression |
|
Password salt |
String |
|
|
|
String |
|
|
Password question |
String |
|
|
Password answer |
String |
|
|
IsApproved |
Bool |
|
|
IsAnonymous |
Bool |
|
|
IsLockedOut |
Bool |
|
|
CreateDate |
DateTime |
In UTC |
|
LastLoginDate |
DateTime |
In UTC |
|
LastPasswordChangeDate |
DateTime |
In UTC |
|
LastLockoutDate |
DateTime |
In UTC |
|
LastActivityDate |
DateTime |
In UTC |
|
FailedPasswordAttemptCount |
Int |
|
|
FailedPasswordAttemptWindowStart |
DateTime |
In UTC |
|
FailedPasswordAnswerAttemptCount |
Int |
|
|
FailedPasswordAnswerAttemptWindowStart |
DateTime |
In UTC |
|
Comment |
String |
|
|
ProfileLastUpdated |
DateTime |
In UTC |
|
ProfileBlobName |
String |
Blob where profile data is stored |
|
ProfileIsCreatedByProfileProvider |
Bool |
|
|
ProfileSize |
Int |
Size of profile data; saved to save roundtrips |
Role Table
|
Column |
Type |
Comment |
|
Partition key |
String |
Concatenation of application name and user name |
|
Row key |
String |
Role name |
|
Application name |
String |
|
|
User name |
String |
|
|
Role name |
String |
|
Session State Table
|
Column |
Type |
Comment |
|
Partition key |
String |
Application name + Session id |
|
Row key |
String |
Not used, empty string |
|
Id |
String |
|
|
Application name |
String |
|
|
Blob name |
String |
References session blob |
|
Timeout |
DateTime |
|
|
Expires |
DateTime |
In Utc |
|
Created |
DateTime |
In Utc |
|
Lock date |
DateTime |
In Utc |
|
Locked |
Bool |
|
|
Lock |
Int |
|
|
Initialized |
bool |
|
浙公網安備 33010602011771號