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也类似,请参考附件中的代码)
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 |
|