您可以通過實施您自己的SiteMembershipProvider
來實現此目的。下面是一個實現我做了一段時間後的一個例子片段:
public class CustomMembershipUser : MembershipUser
{
public UserItem UserItem { get; private set; }
public CustomMembershipUser(string providerName, UserItem user)
: base(providerName, user.UserName, "user_" + user.UserID, user.Email,
null, null, true, !user.IsCurrent, DateTime.MinValue,
DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue)
{
this.UserItem = user;
}
}
public class SiteMembershipProvider : MembershipProvider
{
....
private static MembershipUser GetMembershipUser(UserItem user)
{
return new CustomMembershipUser(_membershipProviderName,user);
}
public override MembershipUser GetUser(string username, bool userIsOnline)
{
// Load user
return GetMembershipUser(foundUser);
}
....
}
在Web.Config中:
<membership defaultProvider="SiteMembershipProvider">
<providers>
<clear/>
<add name="SiteMembershipProvider" type="SiteMembershipProvider" enablePasswordRetrieval="false" enablePasswordReset="false" requiresQuestionAndAnswer="false" connectionStringName="myConnectionString"/>
</providers>
</membership>
這應該默認你的整個網站以使用新的MembershipProvider
稱爲SiteMembershipProvider
。
有a look here或here for an example來實現整個班級。