初学MemberShip,遇到一个Membership设置的问题
因为一般教程使用的示例都是SQL Server 2005 Express版本的本地 ASPNETDB.MDF文件,如果我要使用远程的Sql Server 2005服务器的话,需要进行一些设置。
Setp1:当你使用Membership, 我们可以发现ASP.NET 2.0 已经在 SQL Server 2005 Express建立了很多表(aspnet_Membership等),视图,存储过程等。当时我们自己的远程服务器上并没有这些表。难道我们需要自己建?当然不用,微软给我们准备了一个工具:
%systemroot%\Microsoft.NET\Framework\v2.x\
目录下的aspnet_regsql工具
根据它的向导,可以很方便地指定你的服务器和数据库。完成之后,我们可以发现,我们的远程服务器上制定数据库上已经创建好了我们需要的表,视图,存储过程等。
Step2:这时候我们需要在web.config更改Membership的Provider
我们可以参考machine.config文件中的内容:
(machine.config在%systemroot%\Microsoft.NET\Framework\v2.x\CONFIG目录下)
<
connectionStrings
>
<
add
name
="LocalSqlServer"
connectionString
="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName
="System.Data.SqlClient"
/>
</
connectionStrings
>
<
membership
>
<
providers
>
<
add
name
="AspNetSqlMembershipProvider"
type
="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
connectionStringName
="LocalSqlServer"
enablePasswordRetrieval
="false"
enablePasswordReset
="true"
requiresQuestionAndAnswer
="true"
applicationName
="/"
requiresUniqueEmail
="false"
passwordFormat
="Hashed"
maxInvalidPasswordAttempts
="5"
minRequiredPasswordLength
="7"
minRequiredNonalphanumericCharacters
="1"
passwordAttemptWindow
="10"
passwordStrengthRegularExpression
=""
/>
</
providers
>
</
membership
>
你可以直接在machine.config里更改,不过这里一更改,会影响到以后所有ASP.NET程序的设置,所以我们还是在自己的web.config中更改比较好
在web.config中,首先创建我们自己的连接字符串:
<
connectionStrings
>
<
add
name
="remoteSqlServer"
connectionString
="server=..;uid=..;pwd=..;initial catalog=.."
providerName
="System.Data.SqlClient"
/>
</
connectionStrings
>
然后修改Membership的provider.,修改的时候需要注意一个问题,因为machine.config中已经存在一个provider了,所以你直接增加一个provider的话
<
membership
>
<
providers
>
<
add
name
="RemoteSqlMembershipProvider"
type
="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
connectionStringName
="remoteSqlServer"
enablePasswordRetrieval
="false"
enablePasswordReset
="true"
requiresQuestionAndAnswer
="false"
applicationName
="/"
requiresUniqueEmail
="false"
passwordFormat
="Hashed"
maxInvalidPasswordAttempts
="5"
minRequiredPasswordLength
="7"
minRequiredNonalphanumericCharacters
="1"
passwordAttemptWindow
="10"
passwordStrengthRegularExpression
=""
/>
</
providers
>
</
membership
>
在程序运行中还是会去寻找在machineconfig中定义的AspNetSqlMembershipProvider,因为那个是default的。所以一种选择是,我们把这个新的Provider设置为Default的。
<
membership
defaultProvider
="RemoteSqlMembershipProvider"
>
<
providers
>
<
add
name
="RemoteSqlMembershipProvider"
type
="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
connectionStringName
="remoteSqlServer"
enablePasswordRetrieval
="false"
enablePasswordReset
="true"
requiresQuestionAndAnswer
="false"
applicationName
="/"
requiresUniqueEmail
="false"
passwordFormat
="Hashed"
maxInvalidPasswordAttempts
="5"
minRequiredPasswordLength
="7"
minRequiredNonalphanumericCharacters
="1"
passwordAttemptWindow
="10"
passwordStrengthRegularExpression
=""
/>
</
providers
>
另一种选择是,我们首先clear原来的provider,然后再增加自己的provider(connectiongStringName设置为我们自己的ConnectionString):
<
membership
>
<
providers
>
<
clear
/>
<
add
name
="AspNetSqlMembershipProvider"
type
="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
connectionStringName
="remoteSqlServer"
enablePasswordRetrieval
="false"
enablePasswordReset
="true"
requiresQuestionAndAnswer
="false"
applicationName
="/"
requiresUniqueEmail
="false"
passwordFormat
="Hashed"
maxInvalidPasswordAttempts
="5"
minRequiredPasswordLength
="7"
minRequiredNonalphanumericCharacters
="1"
passwordAttemptWindow
="10"
passwordStrengthRegularExpression
=""
/>
</
providers
>
</
membership
>
这里需要注意的另一个问题就是,provider的名字一定需要是AspNetSqlMembershipProvider,其他名字就会出现这个错误:
Default Membership Provider could not be found.
因为原来已经指定了AspNetSqlMembershipProvider为Default Provider.