向DNN中批量导入用户信息

参考http://blogs.snapsis.com/CommentView,guid,c9d3082b-d7b1-4dfb-8c9c-67c0e864964d.aspx

Importing Users to DotNetNuke

I needed to transfer some users from an old membership database to my DotNetNuke database so I made a quick import routine and thought I would share in case anyone has to do something simular. 

What you need to do is get your users into an XML format like that shown below, and use the attached ImportUsers.aspx page from your DNN website.

<Users> <User> <Username>testuser5</Username> <Email>[email protected]</Email> <FirstName>testuser5</FirstName> <LastName>testuser5</LastName> <Password>testuser5</Password> <Unit></Unit> <Street></Street> <City></City> <Region></Region> <PostalCode></PostalCode> <Country></Country> <Telephone></Telephone> </User> <User> <Username>testuser6</Username> <Email>[email protected]</Email> <FirstName>testuser6</FirstName> <LastName>testuser6</LastName> <Password>testuser6</Password> <Unit></Unit> <Street></Street> <City></City> <Region></Region> <PostalCode></PostalCode> <Country></Country> <Telephone></Telephone> </User> </Users>


I quickly made an XML file like the one above from my User DB by running the Following SQL:

SELECT '<Username>' + UL.Username + '</Username>', '<Email>' + S.Email + '</Email>', '<FirstName>' + S.FirstName + '</FirstName>', '<LastName>' + S.LastName + '</LastName>', '<Password>' + UL.Password + '</Password>' FROM UserLogins UL Left Outer Join Subscribers S On UL.UserName = S.UserName where S.Username is not null

 

 Of course you will probably have different tables in your User DB, but you should get the idea.

As you can see from the SQL above, not all the nodes have to be in the XML for it to work.

我的应用实例

      正如我前几篇文章中写道的,我的情况和上述文章的作者所遇到的情况极其相似,当我看到这篇文章是还是小激动了一把,毕竟找了一天才知道那么一点有用的。信心海量啊,不废话了,做我的总结吧。

      我们将DNN-ImportUsers1.zip解压发现有两个文件,分别是ImportUsers.aspx和ImportUsers.aspx.vb,那么这两个文件怎么使用呢?有人说要自己建模块,然后将这两个文件添加进去。但是我对DNN建模块还很生疏,那么我也有我自己的方法。

       在项目中“添加新项->选择添加 Web窗体->命名为 ImportUsers.aspx-> 然后将 ImportUsers.aspx.vb 中的内容复制过去。”我在期中做了一些修改,就是将命名空间去掉,html重排了一下。代码我回在下面贴出来。

ImportUsers.aspx的代码

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="ImportUsers.aspx.vb" Inherits="ImportUsers" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Import Users</title> </head> <body> <Form id="Form" runat="server" ENCTYPE="multipart/form-data" style="攈eight: 100%;" mce_style="攈eight: 100%;"> <table cellpadding="0" cellspacing="0" border="0"> <tr><td align="center" style="padding-bottom:20px" mce_style="padding-bottom:20px"><h2>DotNetNuke Quick User Import</h2></td></tr> <tr><td width="100%" align="center">Enter your user list in the XML format as shown below (paste your list over the example), and click import users: <asp:TextBox ID="txtUserXML" runat="server" Height="300px" TextMode="MultiLine" Width="650px"> <Users> <User> <Username>testuser5</Username> <Email>[email protected]</Email> <FirstName>testuser5</FirstName> <LastName>testuser5</LastName> <Password>testuser5</Password> <Unit></Unit> <Street></Street> <City></City> <Region></Region> <PostalCode></PostalCode> <Country></Country> <Telephone></Telephone> </User> <User> <Username>testuser6</Username> <Email>[email protected]</Email> <FirstName>testuser6</FirstName> <LastName>testuser6</LastName> <Password>testuser6</Password> <Unit></Unit> <Street></Street> <City></City> <Region></Region> <PostalCode></PostalCode> <Country></Country> <Telephone></Telephone> </User> </Users></asp:TextBox><br /> <br /> <asp:linkbutton class="CommandButton" id="cmdImportUsers" resourcekey="cmdImportUsers" runat="server" text="Import Users"></asp:linkbutton>  </td></tr> <tr><td align="center" style="font-weight:bold;padding-top:20px;color:red" mce_style="font-weight:bold;padding-top:20px;color:red">*** Remember to always backup your data first, and if you are doing this on a live site then you should remove this page when finished. ***</td></tr> </table> </form> </body> </html>

 

ImportUsers.aspx.vb 的代码

Imports AspNetSecurity = System.Web.Security Imports DotNetNuke.Entities.Modules Imports DotNetNuke.Security.Membership Imports DotNetNuke.Services.Mail Imports DotNetNuke.UI.Utilities Imports System.Xml ''' ----------------------------------------------------------------------------- ''' <summary> ''' The ImportUsers PortalModuleBase is used to Import Users ''' </summary> ''' <returns></returns> ''' <remarks> ''' </remarks> ''' <history> ''' [jmitchell] 6/1/2006 Created ''' </history> ''' ----------------------------------------------------------------------------- Partial Class ImportUsers Inherits System.Web.UI.Page #Region "Event Handlers" ''' ----------------------------------------------------------------------------- ''' <summary> ''' cmdImportUsers_Click will import the users defined with XML in the textbox ''' </summary> ''' <remarks> ''' </remarks> ''' <history> ''' [jmitchell] 06/01/2006 created ''' </history> ''' ----------------------------------------------------------------------------- Private Sub cmdImportUsers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdImportUsers.Click Dim xmlDoc As New XmlDocument xmlDoc.LoadXml(txtUserXML.Text) ' update the user record in the database Dim responseBuilder As StringBuilder = New StringBuilder() For Each xmlItem As XmlElement In xmlDoc.SelectNodes("/Users/User") Dim oUserInfo As New UserInfo oUserInfo.PortalID = 0 oUserInfo.Membership.Username = GetXmlItemValue(xmlItem, "Username") oUserInfo.Username = GetXmlItemValue(xmlItem, "Username") oUserInfo.Profile.FirstName = GetXmlItemValue(xmlItem, "FirstName") oUserInfo.FirstName = GetXmlItemValue(xmlItem, "FirstName") oUserInfo.Profile.LastName = GetXmlItemValue(xmlItem, "LastName") oUserInfo.LastName = GetXmlItemValue(xmlItem, "LastName") oUserInfo.Profile.Unit = GetXmlItemValue(xmlItem, "Unit") oUserInfo.Profile.Street = GetXmlItemValue(xmlItem, "Street") oUserInfo.Profile.City = GetXmlItemValue(xmlItem, "City") oUserInfo.Profile.Region = GetXmlItemValue(xmlItem, "Region") oUserInfo.Profile.PostalCode = GetXmlItemValue(xmlItem, "PostalCode") oUserInfo.Profile.Country = GetXmlItemValue(xmlItem, "Country") oUserInfo.Profile.Telephone = GetXmlItemValue(xmlItem, "Telephone") oUserInfo.Membership.Email = GetXmlItemValue(xmlItem, "Email") oUserInfo.Email = GetXmlItemValue(xmlItem, "Email") oUserInfo.Membership.Approved = True oUserInfo.AffiliateID = Null.NullInteger oUserInfo.Membership.Password = GetXmlItemValue(xmlItem, "Password") Dim createStatus As UserCreateStatus = UserController.CreateUser(oUserInfo) If createStatus = UserCreateStatus.Success Then responseBuilder.Append(String.Format("User '{0}' created successfully", oUserInfo.Username)) Else responseBuilder.Append(String.Format("User '{0}' creation failed: {1}", oUserInfo.Username, [Enum].GetName(GetType(UserCreateStatus), createStatus))) End If Next txtUserXML.Text = responseBuilder.ToString() End Sub Private Function GetXmlItemValue(ByVal xmlItem As XMLElement, ByVal nodeName As String) As String If xmlItem(nodeName) Is Nothing Then Return "" Else Return xmlItem(nodeName).InnerText End If End Function #End Region #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region End Class

 

      接下来进入http://localhost/dnn5.2.3/ImportUsers.aspx这个页面,点击 "Import Users“就可以将指定的testuser5和testuser6添加到数据中去了。我们如果使用host账户登录到"User Accounts"中能够找到我们刚才添加的两个账户。这表明添加用户是成功的。

      这里还有比较中的就是passwordFormat的问题了,在<membership defaultProvider="AspNetSqlMembershipProvider">中如果 passwordFormat="Clear" 表示密码是明文,不经过加密,如果   passwordFormat="[Hashed|Encrypted]" 则密码是经过加密以后才存放到数据库中去的。老系统中的密码是用16位MD5加密的,是密文,我们无法知道明文的密码,所以在移植数据库的时候,我们首先要将passwordFormat设置为"Clear"以后才能进行"Import Users"。

      在数据库移植完成以后,我们要做的就是将加密方式改回到“16位MD5加密”,我们这里用"Hashed"来表示。这样用户在登录的时候就可以用自己知道的用户名和密码来登录了。

 

使用SQL查询于都得出xml格式的结果,查询语句如下

SELECT '<User>'+ '<Username>' + [USER_NAME] + '</Username>', '<Email>' + [USER_EMAIL] + '</Email>', '<Password>' + [USER_PASSWORD] + '</Password>' + '</User>' FROM USERSold

 

 

 

你可能感兴趣的:(向DNN中批量导入用户信息)