分享

DotNetNuke与MemberShip的结合 -- 博客园

 kommy 2007-09-13

DotNetNuke与MemberShip的结合

Posted on 2006-07-13 15:05 二十四画生 阅读(3015) 评论(4)  编辑  收藏 所属分类: DotNetNuke(DNN)
         DotNetNuke(以下简称DNN)采用与MemberShip结合的方式实现身份验证功能。DNN4.3.x和DNN3.3.x版本相对于以前版本对用户部分进行了很大的改进。具体结合MemberShip实现身份认证,从数据库上分析如下:

关系图如下:

相关表含义:

aspnet_Users:用户信息表

字段名称

含义

类型

备注

ApplicationId

应用程序ID

Uniqueidentifier

 

UserId

用户ID

Uniqueidentifier

 

Username

用户名

Nvarchar(256)

 

LoweredUserName

 

Nvarchar(256)

 

LastName

 

Nvarchar(50)

 

MobileAlias

 

Nvarchar(16)

 

IsAnonymous

 

Bit

 

LastActivityDate

 

Datetime

 

 

aspnet_Membership:成员信息表

字段名称

含义

类型

备注

ApplicationId

应用程序ID

Uniqueidentifier

 

UserId

用户ID

Uniqueidentifier

 

Password

密码

Nvarchar(128)

 

PasswordFormat

密码加密方式

Int

 

PasswordSalt

密码散列

Nvarchar(128)

 

----------------

 

 

 

其余的字段关系不大,主要是结合Membership功能的

-----------------

 

 

 

 

UsersDNN用户表

字段名称

含义

类型

备注

UserID

用户ID

Int

 

Username

用户名

Nvarchar(100)

 

FirstName

Nvarchar(50)

 

LastName

Nvarchar(50)

 

IsSuperUser

是否是超级用户

Bit

 

AffiliateId

联盟商ID

Int

会员介绍人ID

Email

电子邮件地址

Nvarchar(256)

 

DisplayName

昵称

Nvarchar(128)

 

UpdatePassword

是否要求用户定期更新密码

Bit

 

 

UserProfile:用户资料表(存储用户详细资料,如:地址信息,联系方式等)

字段名称

含义

类型

备注

ProfileID

用户资料ID

Int

 

UserID

用户ID

Int

 

PropertyDefinitionID

用户资料信息定义ID

Int

 

PropertyValue

用户资料信息值

Nvarchar(3750)

 

PropertyText

---------

 

 

Visibility

可见性

Int

0:公开;1:保密;2:管理员可见

LastUpdatedDate

最后更新时间

Datetime

 

 

ProfilePropertyDefinition:个人资料属性定义

字段名称

含义

类型

备注

PropertyDefinitionID

个人资料属性定义ID

Int

 

PortalID

站点ID

Int

 

ModuleDefID

模块定义ID

Int

指定该属性可以属于一个的制定的模块,不指定值为-1

Deleted

是否删除

Bit

 

DataType

数据类型

Int

 

DefaultValue

默认值

Nvarchar(50)

 

PropertyCategory

属性分类

Nvarchar(50)

 

PropertyName

属性名称

Nvarchar(50)

 

Length

属性值长度

Int

 

Required

是否必添

Bit

 

ValidationExpression

验证规则

Nvarchar(100)

 

ViewOrder

排序号

Int

 

Visible

是否可见

Bit

 

  

 

DotNetNuke与MemberShip的结合(五年版)

Posted on 2007-08-13 21:01 五年 阅读(1597) 评论(3)  编辑  收藏 所属分类: DotNetNuke

24画生有一篇同名文章(http://www.cnblogs.com/esshs/archive/2006/07/13/449888.html),但他只是列一下数据库表与字段,并没有展开讲述.

下面我以DNN4.5.5为源码版本站在24画生的肩旁上再展开讲述.

 

DNN_membership1 

DNN_membership

由上面三图可见,DNN系统设计中的MemberShip是完全独立于DNN其他表的,那么能不能将整个MemberShip独立放一个表中,或直接使用已经存在的MemberShip表?由于上面的表是相对独立的,没有直接关系,所以理论上是可以的.实际上可以吗?做个实验.

 在web.config中增加一个数据连接字符串.SiteSqlServer1,并且指向已经存在的MemberShip数据库ASPNETDB,其中DNN4_5_5是空数据库,即DNN并未安装,ASPNETDB是一个现存系统的MemberShip数据库.

 <connectionStrings>
    <!-- Connection String for SQL Server 2005 Express
    <add
      name="SiteSqlServer"
      connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|Database.mdf;"
      providerName="System.Data.SqlClient" />
    -->
    <!-- Connection String for SQL Server 2000/2005 -->
    <add name="SiteSqlServer" connectionString="Data Source=(local);Initial Catalog=DNN4_5_5;User ID=sa;Password=" providerName="System.Data.SqlClient" />
    <add name="SiteSqlServer1" connectionString="Data Source=(local);Initial Catalog=ASPNETDB;User ID=sa;Password=" providerName="System.Data.SqlClient" />
  </connectionStrings>

将AspNetSqlMembershipProvider的配置connectionStringName="SiteSqlServer"改成 connectionStringName="SiteSqlServer1"如下.

    <membership defaultProvider="AspNetSqlMembershipProvider" userIsOnlineTimeWindow="15">
      <providers>
        <clear />
        <!-- Configuration for DNNSQLMembershipProvider:
              connectionStringName="string"               Name corresponding to the entry in <connectionStrings> section where the connection string for the provider is specified
              passwordAttemptThreshold="int"              The number of failed password attempts, or failed password answer attempts that are allowed before locking out a user?s account
              passwordAttemptWindow="int"                 The time window, in minutes, during which failed password attempts and failed password answer attempts are tracked
              enablePasswordRetrieval="[true|false]"      Should the provider support password retrievals
              enablePasswordReset="[true|false]"          Should the provider support password resets
              requiresQuestionAndAnswer="[true|false]"    Should the provider require Q & A
                    minRequiredPasswordLength="int"                  The minimum password length
                    minRequiredNonalphanumericCharacters="int"  The minimum number of non-alphanumeric characters
              applicationName="string"                    Optional string to identity the application: defaults to Application Metabase path
              requiresUniqueEmail="[true|false]"          Should the provider require a unique email to be specified
              passwordFormat="[Clear|Hashed|Encrypted]"   Storage format for the password: Hashed (SHA1), Clear or Encrypted (Triple-DES)
              description="string"                        Description of what the provider does
        -->
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="SiteSqlServer1" enablePasswordRetrieval="true" enablePasswordReset="true" requiresQuestionAndAnswer="false" minRequiredPasswordLength="7" minRequiredNonalphanumericCharacters="0" requiresUniqueEmail="false" passwordFormat="Encrypted" applicationName="/" description="Stores and retrieves membership data from the local Microsoft SQL Server database" />
      </providers>
    </membership>

这样运行的结果是:

DNN成功安装,DNN4_5_5数据库中有MemberShip的所有表,但是里面全为空,host,admin的账号在ASPNETDB的MemberShip表中,用host,admin账号登录成功.DNN运行一切正常.

这面意味着:DNN可以很方便地与其他同样以MemberShip为身份验证的系统整合.

再分析相关代码:

AspNetMembershipProvider

举例: 删除用户方法

Public Overrides Function DeleteUser(ByVal user As UserInfo) As Boolean

    Dim retValue As Boolean = True
    Dim dr As IDataReader

    Try
        dr = dataProvider.GetRolesByUser(user.UserID, user.PortalID)
        While dr.Read
            dataProvider.DeleteUserRole(user.UserID, Convert.ToInt32(dr("RoleId")))
        End While
        dr.Close()

        ‘check if user exists in any other portal
        dr = dataProvider.GetUserByUsername(-1, user.Username)
        dr.Read()
        If Not dr.Read Then
            dataProvider.DeleteUser(user.UserID) ‘ 删除dnn_User表中的用户记录

            ‘Delete AspNet MemrshipUser
            retValue = DeleteMembershipUser(user)  ‘删除Membership表中的用户记录(至于Membership相关表在哪里,在这里是透明的,DNN并不在意他在哪里.)
        Else
            dataProvider.DeleteUserPortal(user.UserID, user.PortalID)
        End If
        dr.Close()
    Catch ex As Exception
        retValue = False
    End Try

    Return retValue

End Function

Private Function DeleteMembershipUser(ByVal user As UserInfo) As Boolean
    Dim retValue As Boolean = True
    Try

‘删除Membership表中的用户记录(至于Membership相关表在哪里,在这里是透明的,DNN并不在意他在哪里.)
        AspNetSecurity.Membership.DeleteUser(user.Username, True)
    Catch ex As Exception
        retValue = False
    End Try
    Return retValue
End Function

我比较喜欢这样的设计,因为这样,不管多少系统,只要基于MemberShip就可以很方便地整合在一起.实现单点登陆.



本文发布于:http://shiningrise.cnblogs.com
转载请保留此签名.
 
 
 

添加membership用户到DNN数据库

Posted on 2007-08-22 00:47 五年 阅读(1241) 评论(4)  编辑  收藏 所属分类: DotNetNuke

在我的 <<DotNetNukeMemberShip的结合(五年版)>>中提出将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中的用户.而以<<DotNetNukeMemberShip的结合(五年版)>>办法安装的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>

 

源码下载:点击下载

相关文章: <<DotNetNukeMemberShip的结合(五年版)>>
http://www.cnblogs.com/shiningrise/archive/2007/08/13/854297.html



本文发布于:http://shiningrise.cnblogs.com
转载请保留此签名.
 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多