如何解决论坛跨域的登录,退出的

[ 2318 查看 / 4 回复 ]

装的是2.5Beta版 论坛域名是:forum.web.net(虚拟) 网站域名是:www.web.net(虚拟too) 先用管理员帐户进入论坛后台在基本设置中,将“身份验证Cookie域:”设置为.web.net 论坛配置好后, 复制bin文件夹的Discuz.Forum.dll,Discuz.Entity.dll,Discuz.Data.dll,Discuz.Data.SqlServer.dll和Discuz.Common.dll到网站的bin下。 同样,DNT.Config也放到网站根目录 贴个代码看,更清楚些。 using System; using System.Collections; using System.Configuration; using System.Data; using System.Web; using Discuz.Forum; using Discuz.Common; using Discuz.Entity; namespace WebSite {     public partial class _Default : System.Web.UI.Page     {         protected void Page_Load(object sender, EventArgs e)         {             //下面的判断作用是:检测cookie中是不是有论坛的userid,如果有就说明有用户登录             if (Request.Cookies["dnt"] != null && Request.Cookies["dnt"].Values["userid"] != null)             {                 //从cookie中获得UserID                 int uid = Convert.ToInt32(Request.Cookies["dnt"].Values["userid"].ToString());                 //得到这个用户的全部信息                 UserInfo a = Discuz.Forum.Users.GetUserInfo(uid);                 //打印出来看看对不对。                 Response.Write(a.Username);                 //这句代码是退出!清除Cookie!!!                 ForumUtils.ClearUserCookie();             }             else //没有用户?看我怎么登录的!             {                 //先设俩变量                 string uname = "rohan";                 string upass = "mypassword";                 //验证用户登录 如果正确返回UserID,否则返回-1                 int uid = Users.CheckPassword(uname, upass, true);                 if (uid!=-1)                 {                     //下面都是官方整合说明文档里的内容了                     LoginLogs.DeleteLoginLog(DNTRequest.GetIP());                     //根据积分公式刷新用户总积分                     UserCredits.UpdateUserCredits(uid);                     //写入用户登录后的cookie                     //××××××注意××××××这里有点特殊,原文是                     //ForumUtils.WriteUserCookie(uid, Utils.StrToInt(DNTRequest.GetString("expires"), -1), config.Passwordkey, DNTRequest.GetInt("templateid", 0), DNTRequest.GetInt("loginmode", -1));                     //这里我把config.Passwordkey直接从论坛config文件夹下的general.config文件的<Passwordkey />节点提取出来了。这样就不用把论坛的相关文件拷贝到网站上了                     ForumUtils.WriteUserCookie(uid, Utils.StrToInt(DNTRequest.GetString("expires"), -1), "R254842J4Z", DNTRequest.GetInt("templateid", 0), DNTRequest.GetInt("loginmode", -1));                     //更新该用户最后访问时间                     Users.UpdateUserLastvisit(uid, DNTRequest.GetIP());                 }             }                     }     } }
分享 转发
TOP

偷懒的方式就是把设置好的config文件夹和cache文件夹也拷贝过来。 dnt.config不用设置。只要从安装好的bbs中拷贝过来就行。 只需要把dnt的bin目录里的DLL复制到网站的bin目录 然后在需要整合的库里引用 Discuz.Common Discuz.Config Discuz.Entity Discuz.Forum 四个DLL 其余操作,均按官方的文档操作即可.

附件: dnt_2_userapidoc.rar (2008-8-30 9:28:26, 19.10 K)
该附件被下载次数 53

仔细看官方的整合说明文档 还要把dnt/config目录里的所有文件,全部COPY到网站config目录 把DNT的bin目录的文件全部COPY到网站bin目录 dnt.config也要放到网站根目录 还要在网站根目录建立一个cache目录
TOP

1:网站和论坛的数据库不一样(网站是dujia数据库www.,论坛是bbs数据库bbs.),而且会员信息表字段也不一样,所以要保持会员在网站和论坛上的同步登陆,退出,和注册,并且资料修改也需要同步确实是一个难题。 2:考虑到以后公司不用这个论坛了,得很容易地分离网站和论坛。不能做很大修改。 通过学习社区动力的相关资料,确定如下的解决方案,并且测试成功(),^_^,兴奋之余,也写出来和大家一起分享。 一、数据库的整合,主要利用触发器技术实现两个数据库之间的会员表信息修改、添加、删除的同步,其中网站会员表:HotelUser;论坛相关的会员数据表有:dnt_users,dnt_userfields,dnt_statistics。论坛相关的数据表的作用可参考论坛官方网站说明,主要步骤如下: 1:修改dnt_users的数据设计,把uid的标识规范修改为否。如下图 01.gif (28.12 K) 2008-8-13 13:39:23 2:修改存储过程dnt_createuser。修改为(主要是修改udid的值) set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO 。。。。中间的不变,节约篇幅。。。 AS DECLARE @uid int select @uid=max(UserId)+1 from dujia.[dbo].[HotelUser] INSERT INTO [dnt_users]([uid],[username],[nickname], [password], [secques], [gender], [adminid], [groupid], [groupexpiry], [extgroupids], [regip], [joindate], [lastip], [lastvisit], [lastactivity], [lastpost], [lastpostid], [lastposttitle], [posts], [digestposts], [oltime], [pageviews], [credits], [extcredits1], [extcredits2], [extcredits3], [extcredits4], [extcredits5], [extcredits6], [extcredits7], [extcredits8], [avatarshowid], [email], [bday], [sigstatus], [tpp], [ppp], [templateid], [pmsound], [showemail], [newsletter], [invisible], [newpm], [accessmasks]) VALUES(@uid,@username,@nickname, @password, @secques, @gender, @adminid, @groupid, @groupexpiry, @extgroupids, @regip, @joindate, @lastip, @lastvisit, @lastactivity, @lastpost, @lastpostid, @lastposttitle, @posts, @digestposts, @oltime, @pageviews, @credits, @extcredits1, @extcredits2, @extcredits3, @extcredits4, @extcredits5, @extcredits6, @extcredits7, @extcredits8, @avatarshowid, @email, @bday, @sigstatus, @tpp, @ppp, @templateid, @pmsound, @showemail, @newsletter, @invisible, @newpm, @accessmasks) IF @@ERROR=0         BEGIN                 UPDATE [dnt_statistics] SET [totalusers]=[totalusers] + 1,[lastusername]=@username,[lastuserid]=@uid         END INSERT INTO dnt_userfields         ([uid],[website],[icq],[qq],[yahoo],[msn],[skype],[location],[customstatus],[avatar],[avatarwidth],[avatarheight],[medals],[bio],[signature],[sightml],[authstr],[realname],[idcard],[mobile],[phone]) VALUES         (@uid,@website,@icq,@qq,@yahoo,@msn,@skype,@location,@customstatus,@avatar,@avatarwidth,@avatarheight,@medals,@bio,@signature,@sightml,@authstr,@realname,@idcard,@mobile,@phone) 3:增加触发器,保持会员信息的同步,首先得在自己的会员表信息增加触发器, 02.gif (11.21 K) 2008-8-13 13:39:23 然后在论坛上dnt_users,dnt_userfields分别增加触发器, 03.gif (11.60 K) 2008-8-13 13:39:23 04.gif (10.11 K) 2008-8-13 13:39:23 由于不同的用户表的设计不一样,我只把网站增加会员的触发器发出来,供参考 set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO ALTER trigger [dbo].[user_insert] on [dbo].[HotelUser] for Insert as  ---使论坛中的触发器暂时失效 ALTER table bbs.[dbo].[dnt_users] DISABLE TRIGGER dnt_users_insert ---取得插入用户数据的ID和用户名 Declare @uid int select @uid=max(UserID) from HotelUser declare @username nchar(20) select @username=UserName from HotelUser ---插入论坛用户基本数据 insert into bbs.[dbo].[dnt_users]([uid],[username],[password],[gender],[email]) select HotelUser.UserID,i.UserName,i.UserPassword, case i.UserSex   when 'f' then 2   when 'm' then 1   when 's' then 0 end ,i.UserEmail from inserted as i inner join HotelUser on HotelUser.UserName=i.UserName --用户身份变成注册会员 update bbs.[dbo].[dnt_users] set [groupid]=10 where uid=@uid ---插入论坛用户详细数据 INSERT INTO bbs.[dbo].dnt_userfields         ([uid],[qq],[msn],[location],[realname],[idcard],[mobile],[phone]) select HotelUser.UserID,i.UserQQ,i.UserMSN,i.UserAddress,i.UserRealName,i.UserIdCard,i.UserMobile,i.UserTel from inserted as i inner join HotelUser on HotelUser.UserName=i.UserName ---修改论坛状态 UPDATE bbs.[dbo].[dnt_statistics] SET [totalusers]=[totalusers] + 1,[lastusername]=@username,[lastuserid]=@uid ---使论坛中的触发器生效 ALTER table bbs.[dbo].[dnt_users] ENABLE TRIGGER dnt_users_insert ---发放积分 insert into dujia.[dbo].[HotelUserPoint](PointAction,PointChange,PointUser) values('注册会员',500,@uid) 经过上面的三步,数据的整合已经完成,会员的信息可以保持同步修改,删除,增加,哈哈。一大功劳吧。下面主要是程序上的整合。 二、程序上的整合:程序上的整合主要依据了论坛的登陆,判断是否登陆,退出的原理。论坛的登陆采用了自己的cookies 验证。对密码的加密先md5加密.然后采用DES加密,其中加密密钥是在论坛安装时随机生成给每位用户的,该加密密钥在文件general.config里面.  <Passwordkey>AAAAAAAAAA</Passwordkey>.这样其实保证了用户密码的安全性。 废话不多说,现在就开始程序上的整合。   1:由于是跨域验证,先在论坛后台设置如下: 05.gif (3.52 K) 2008-8-13 13:39:23 2:从论坛的DES加密文件中提取自己所需要的 DES加密函数 namespace dujia.DBUtility {     ///论坛加密算法     public class dntDES     {         //默认密钥向量         private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };         /// <summary>         /// DES加密字符串         /// </summary>         /// <param name="encryptString">待加密的字符串</param>         /// <param name="encryptKey">加密密钥,要求为8位</param>         /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>         public static string Encode(string encryptString, string encryptKey)         {             encryptKey = "你论坛加密密钥";             encryptKey = GetSubString(encryptKey, 8, "");             encryptKey = encryptKey.PadRight(8, ' ');             byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));             byte[] rgbIV = Keys;             byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);             DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();             MemoryStream mStream = new MemoryStream();             CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);             cStream.Write(inputByteArray, 0, inputByteArray.Length);             cStream.FlushFinalBlock();             return Convert.ToBase64String(mStream.ToArray());         }   }   3:会员登陆,先在网站的数据库验证用户的合法性,再写模仿论坛写cookies if (bllHotelUser.checkUserLogin(userLogin, md5Password, out userId))//网站用户验证 {     string dntPassword = HttpUtility.UrlEncode(dujia.DBUtility.dntDES.Encode(md5Password, ""));     HttpCookie cookie = new HttpCookie("dnt");     cookie.Values["userid"] = userId.ToString();     cookie.Values["password"] = dntPassword;     cookie.Values["tpp"] = "0";     cookie.Values["ppp"] = "0";     cookie.Values["pmsound"] = "0";     cookie.Values["invisible"] = "0";     cookie.Values["referer"] = "index.aspx";     cookie.Values["sigstatus"] = "0";     cookie.Values["expires"] = "100";     cookie.Expires = DateTime.Now.AddMinutes(10);     cookie.Domain = ".dujia168.com";     Response.AppendCookie(cookie);     Response.Redirect("/account/index.aspx"); } else {     LTP.Common.MessageBox.Show(this, "用户名或者密码错误,请核对之后再登陆!!"); } 这样网站登陆之后。论坛也就同时登陆了。     4:网站的登陆验证,这个过程主要是读取cookies存储的用户id和密码信息。然后从网站数据库提取的数据做比较。一致则用户是登陆状态,这步就保证了用户从论坛或者网站登陆时,网站能判断用户的登陆状态。     5:网站用户退出。主要是清除cookies值,重设下论坛的状态。
TOP

1:网站和论坛的数据库不一样(网站是dujia数据库www.,论坛是bbs数据库bbs.),而且会员信息表字段也不一样,所以要保持会员在网站和论坛上的同步登陆,退出,和注册,并且资料修改也需要同步确实是一个难题。 2:考虑到以后公司不用这个论坛了,得很容易地分离网站和论坛。不能做很大修改。 通过学习社区动力的相关资料,确定如下的解决方案,并且测试成功(),^_^,兴奋之余,也写出来和大家一起分享。 一、数据库的整合,主要利用触发器技术实现两个数据库之间的会员表信息修改、添加、删除的同步,其中网站会员表:HotelUser;论坛相关的会员数据表有:dnt_users,dnt_userfields,dnt_statistics。论坛相关的数据表的作用可参考论坛官方网站说明,主要步骤如下: 1:修改dnt_users的数据设计,把uid的标识规范修改为否。如下图 01.gif (28.12 K) 2008-8-13 13:39:23 2:修改存储过程dnt_createuser。修改为(主要是修改udid的值) set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO 。。。。中间的不变,节约篇幅。。。 AS DECLARE @uid int select @uid=max(UserId)+1 from dujia.[dbo].[HotelUser] INSERT INTO [dnt_users]([uid],[username],[nickname], [password], [secques], [gender], [adminid], [groupid], [groupexpiry], [extgroupids], [regip], [joindate], [lastip], [lastvisit], [lastactivity], [lastpost], [lastpostid], [lastposttitle], [posts], [digestposts], [oltime], [pageviews], [credits], [extcredits1], [extcredits2], [extcredits3], [extcredits4], [extcredits5], [extcredits6], [extcredits7], [extcredits8], [avatarshowid], [email], [bday], [sigstatus], [tpp], [ppp], [templateid], [pmsound], [showemail], [newsletter], [invisible], [newpm], [accessmasks]) VALUES(@uid,@username,@nickname, @password, @secques, @gender, @adminid, @groupid, @groupexpiry, @extgroupids, @regip, @joindate, @lastip, @lastvisit, @lastactivity, @lastpost, @lastpostid, @lastposttitle, @posts, @digestposts, @oltime, @pageviews, @credits, @extcredits1, @extcredits2, @extcredits3, @extcredits4, @extcredits5, @extcredits6, @extcredits7, @extcredits8, @avatarshowid, @email, @bday, @sigstatus, @tpp, @ppp, @templateid, @pmsound, @showemail, @newsletter, @invisible, @newpm, @accessmasks) IF @@ERROR=0         BEGIN                 UPDATE [dnt_statistics] SET [totalusers]=[totalusers] + 1,[lastusername]=@username,[lastuserid]=@uid         END INSERT INTO dnt_userfields         ([uid],[website],[icq],[qq],[yahoo],[msn],[skype],[location],[customstatus],[avatar],[avatarwidth],[avatarheight],[medals],[bio],[signature],[sightml],[authstr],[realname],[idcard],[mobile],[phone]) VALUES         (@uid,@website,@icq,@qq,@yahoo,@msn,@skype,@location,@customstatus,@avatar,@avatarwidth,@avatarheight,@medals,@bio,@signature,@sightml,@authstr,@realname,@idcard,@mobile,@phone) 3:增加触发器,保持会员信息的同步,首先得在自己的会员表信息增加触发器, 02.gif (11.21 K) 2008-8-13 13:39:23 然后在论坛上dnt_users,dnt_userfields分别增加触发器, 03.gif (11.60 K) 2008-8-13 13:39:23 04.gif (10.11 K) 2008-8-13 13:39:23 由于不同的用户表的设计不一样,我只把网站增加会员的触发器发出来,供参考 set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO ALTER trigger [dbo].[user_insert] on [dbo].[HotelUser] for Insert as  ---使论坛中的触发器暂时失效 ALTER table bbs.[dbo].[dnt_users] DISABLE TRIGGER dnt_users_insert ---取得插入用户数据的ID和用户名 Declare @uid int select @uid=max(UserID) from HotelUser declare @username nchar(20) select @username=UserName from HotelUser ---插入论坛用户基本数据 insert into bbs.[dbo].[dnt_users]([uid],[username],[password],[gender],[email]) select HotelUser.UserID,i.UserName,i.UserPassword, case i.UserSex   when 'f' then 2   when 'm' then 1   when 's' then 0 end ,i.UserEmail from inserted as i inner join HotelUser on HotelUser.UserName=i.UserName --用户身份变成注册会员 update bbs.[dbo].[dnt_users] set [groupid]=10 where uid=@uid ---插入论坛用户详细数据 INSERT INTO bbs.[dbo].dnt_userfields         ([uid],[qq],[msn],[location],[realname],[idcard],[mobile],[phone]) select HotelUser.UserID,i.UserQQ,i.UserMSN,i.UserAddress,i.UserRealName,i.UserIdCard,i.UserMobile,i.UserTel from inserted as i inner join HotelUser on HotelUser.UserName=i.UserName ---修改论坛状态 UPDATE bbs.[dbo].[dnt_statistics] SET [totalusers]=[totalusers] + 1,[lastusername]=@username,[lastuserid]=@uid ---使论坛中的触发器生效 ALTER table bbs.[dbo].[dnt_users] ENABLE TRIGGER dnt_users_insert ---发放积分 insert into dujia.[dbo].[HotelUserPoint](PointAction,PointChange,PointUser) values('注册会员',500,@uid) 经过上面的三步,数据的整合已经完成,会员的信息可以保持同步修改,删除,增加,哈哈。一大功劳吧。下面主要是程序上的整合。 二、程序上的整合:程序上的整合主要依据了论坛的登陆,判断是否登陆,退出的原理。论坛的登陆采用了自己的cookies 验证。对密码的加密先md5加密.然后采用DES加密,其中加密密钥是在论坛安装时随机生成给每位用户的,该加密密钥在文件general.config里面.  <Passwordkey>AAAAAAAAAA</Passwordkey>.这样其实保证了用户密码的安全性。 废话不多说,现在就开始程序上的整合。   1:由于是跨域验证,先在论坛后台设置如下: 05.gif (3.52 K) 2008-8-13 13:39:23 2:从论坛的DES加密文件中提取自己所需要的 DES加密函数 namespace dujia.DBUtility {     ///论坛加密算法     public class dntDES     {         //默认密钥向量         private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };         /// <summary>         /// DES加密字符串         /// </summary>         /// <param name="encryptString">待加密的字符串</param>         /// <param name="encryptKey">加密密钥,要求为8位</param>         /// <returns>加密成功返回加密后的字符串,失败返回源串</returns>         public static string Encode(string encryptString, string encryptKey)         {             encryptKey = "你论坛加密密钥";             encryptKey = GetSubString(encryptKey, 8, "");             encryptKey = encryptKey.PadRight(8, ' ');             byte[] rgbKey = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));             byte[] rgbIV = Keys;             byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);             DESCryptoServiceProvider dCSP = new DESCryptoServiceProvider();             MemoryStream mStream = new MemoryStream();             CryptoStream cStream = new CryptoStream(mStream, dCSP.CreateEncryptor(rgbKey, rgbIV), CryptoStreamMode.Write);             cStream.Write(inputByteArray, 0, inputByteArray.Length);             cStream.FlushFinalBlock();             return Convert.ToBase64String(mStream.ToArray());         }   }   3:会员登陆,先在网站的数据库验证用户的合法性,再写模仿论坛写cookies if (bllHotelUser.checkUserLogin(userLogin, md5Password, out userId))//网站用户验证 {     string dntPassword = HttpUtility.UrlEncode(dujia.DBUtility.dntDES.Encode(md5Password, ""));     HttpCookie cookie = new HttpCookie("dnt");     cookie.Values["userid"] = userId.ToString();     cookie.Values["password"] = dntPassword;     cookie.Values["tpp"] = "0";     cookie.Values["ppp"] = "0";     cookie.Values["pmsound"] = "0";     cookie.Values["invisible"] = "0";     cookie.Values["referer"] = "index.aspx";     cookie.Values["sigstatus"] = "0";     cookie.Values["expires"] = "100";     cookie.Expires = DateTime.Now.AddMinutes(10);     cookie.Domain = ".dujia168.com";     Response.AppendCookie(cookie);     Response.Redirect("/account/index.aspx"); } else {     LTP.Common.MessageBox.Show(this, "用户名或者密码错误,请核对之后再登陆!!"); } 这样网站登陆之后。论坛也就同时登陆了。     4:网站的登陆验证,这个过程主要是读取cookies存储的用户id和密码信息。然后从网站数据库提取的数据做比较。一致则用户是登陆状态,这步就保证了用户从论坛或者网站登陆时,网站能判断用户的登陆状态。     5:网站用户退出。主要是清除cookies值,重设下论坛的状态。
TOP

无聊啊















7k7k小游戏
TOP