添加membership用户到DNN数据库
在我的 <<DotNetNuke与MemberShip的结合(五年版)>>中提出将DNN的身份认证指出现在的membership数据库.而且,admin,host用户可以正常登录.但我们用membership数据库中现在有的用户登录,就会发现不能成功登录.
看相关源码:Provider.Membership.AspNetProvider(AspNetMembershipProvider.vb)
Public Overrides Function UserLogin(ByVal portalId As Integer, ByVal username As String, ByVal password As String, ByVal verificationCode As String, ByRef loginStatus As UserLoginStatus) As UserInfo
'For now, we are going to ignore the possibility that the User may exist in the
'Global Data Store but not in the Local DataStore ie. A shared Global Data Store
'Initialise Login Status to Failure
loginStatus = UserLoginStatus.LOGIN_FAILURE
'Get a light-weight (unhydrated) DNN User from the Database, we will hydrate it later if neccessary
Dim user As UserInfo = Nothing
user = GetUserByUserName(portalId, username, False)
If Not user Is Nothing Then
'Get AspNet MembershipUser
Dim aspnetUser As AspNetSecurity.MembershipUser = Nothing
aspnetUser = GetMembershipUser(user)
'Fill Membership Property from AspNet MembershipUser
FillUserMembership(aspnetUser, user)
'Check if the User is Locked Out (and unlock if AutoUnlock has expired)
If aspnetUser.IsLockedOut Then
Dim intTimeout As Integer = Null.NullInteger
If Not HostSettings("AutoAccountUnlockDuration") Is Nothing Then
Dim strTimeout As String = Convert.ToString(HostSettings("AutoAccountUnlockDuration"))
If Not String.IsNullOrEmpty(strTimeout) Then
intTimeout = Convert.ToInt32(strTimeout)
End If
End If
If intTimeout <> 0 Then
If intTimeout = Null.NullInteger Then
intTimeout = 10
End If
If aspnetUser.LastLockoutDate < Date.Now.AddMinutes(-1 * intTimeout) Then
'Unlock User
user.Membership.LockedOut = False
'Persist to Data Store
aspnetUser.UnlockUser()
Else
loginStatus = UserLoginStatus.LOGIN_USERLOCKEDOUT
End If
End If
End If
'Check in a verified situation whether the user is Approved
If user.Membership.Approved = False And user.IsSuperUser = False Then
'Check Verification code
If verificationCode = (portalId.ToString & "-" & user.UserID) Then
'Approve User
user.Membership.Approved = True
'Persist to Data Store
UpdateUser(user)
Else
loginStatus = UserLoginStatus.LOGIN_USERNOTAPPROVED
End If
End If
'Verify User Credentials
Dim bValid As Boolean = False
If loginStatus <> UserLoginStatus.LOGIN_USERLOCKEDOUT And loginStatus <> UserLoginStatus.LOGIN_USERNOTAPPROVED Then
If user.IsSuperUser Then
If ValidateUser(Null.NullInteger, username, password) Then
loginStatus = UserLoginStatus.LOGIN_SUPERUSER
bValid = True
End If
Else
If ValidateUser(portalId, username, password) Then
loginStatus = UserLoginStatus.LOGIN_SUCCESS
bValid = True
End If
End If
End If
If Not bValid Then
'Clear the user object
user = Nothing
End If
End If
Return user
End Function
其中有三句关键
Dim user As UserInfo = Nothing
user = GetUserByUserName(portalId, username, False)
If Not user Is Nothing Then
意思就是,要先读取DNN用户数据表中的用户即dnn_Users中的用户.而以<<DotNetNuke与MemberShip的结合(五年版)>>办法安装的DNN数据库,在membership中原有的用户,在dnn_Users中却并不存在相应的用户,所以出现用membership中原有的用户无法登录DNN.
我作了下面修正,让登录DNN的用户如果只存在于membership中而不存在于dnn_Users中的数据自动添加.
改到处关键原码
Public Overrides Function UserLogin(ByVal portalId As Integer, ByVal username As String, ByVal password As String, ByVal verificationCode As String, ByRef loginStatus As UserLoginStatus) As UserInfo
'For now, we are going to ignore the possibility that the User may exist in the
'Global Data Store but not in the Local DataStore ie. A shared Global Data Store
'Initialise Login Status to Failure
loginStatus = UserLoginStatus.LOGIN_FAILURE
'Get AspNet MembershipUser
Dim aspnetUser As AspNetSecurity.MembershipUser = Nothing
aspnetUser = GetMembershipUser(username)
If aspnetUser Is Nothing Then
Return Nothing
End If
'Get a light-weight (unhydrated) DNN User from the Database, we will hydrate it later if neccessary
Dim user As UserInfo = Nothing
user = GetUserByUserName(portalId, username, False)
If (user Is Nothing) Then
按照一个AspNetMembershipProvider新建一个项目
Public Class AspNetMembershipProviderFiveYears
Inherits DotNetNuke.Security.Membership.MembershipProvider
改web.config增加一个AspNetMembershipProvider,并设置为defaultProvider
<members defaultProvider="AspNetMembershipProviderFiveYears">
<providers>
<clear />
<add name="AspNetMembershipProvider" type="DotNetNuke.Security.Membership.AspNetMembershipProvider, DotNetNuke.Provider.AspNetProvider" providerPath="~\Providers\MembershipProviders\AspNetMembershipProvider\" />
<add name="AspNetMembershipProviderFiveYears" type="DotNetNuke.Security.Membership.AspNetMembershipProviderFiveYears, DotNetNuke.Provider.AspNetProviderFiveYears" providerPath="~\Providers\MembershipProviders\AspNetMembershipProviderFiveYears\" />
</providers>
</members>
相关文章: <<DotNetNuke与MemberShip的结合(五年版)>>
http://www.cnblogs.com/shiningrise/archive/2007/08/13/854297.html