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">< br>
<providers>
<clear/>
<add name="TableStorageMembershipProvider"
type="Microsoft.Samples.ServiceHosting.AspProviders.TableStorageMembershipProvider"
description="M embership provider using table storage"
applicationName="ProviderTest"
tableServiceB aseUri=」your table service endpoint」
allowInsecureRemoteEndpoints=」false」
accountNam e="youraccountname"
sharedKey="yoursharedkey"
membershipTableName="Membership"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnsw er="false"
minRequiredPasswordLength="1"
minRequiredNonAlphanumericCharacters="0"
requiresUniqueEmail="true"
passwordFormat="Hashed"
/>
</providers>
</membership>
代碼下載:
使用Azure Provider的示例代碼:HTTP://file.ddvip.com/2009_03/1237975802_ddvip_6085.rar
Azure Provider原始程式碼:HTTP://file.ddvip.com/2009_03/1237975847_ddvip_5154.rar
StorageClient:原始程式碼下載:
HTTP://file.ddvip.com/2009_03/1237975906_ddvip_8708.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
Email
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