ASP.NET网站与Discuz!NT论坛整合

1.引入5个DLL到项目中

分别是

using Discuz.Common;
using Discuz.Forum;
using Discuz.Config;
using Discuz.Entity;

using Discuz.Data;

 

2

 

   public class Discuz_NT
    {

        public static ArrayList Login(string username, string password, int question, string answer)
        {
            Discuz.Config.GeneralConfigInfo config = Discuz.Config.GeneralConfigs.GetConfig();
            OnlineUserInfo oluserinfo = OnlineUsers.UpdateInfo(config.Passwordkey, config.Onlinetimeout);
            int olid = oluserinfo.Olid;

            ArrayList result = new ArrayList();
            int uid = -1;
            //如果设置中Discuz!NT的密码模式为动网密码兼容模式(一般仅用于从动网数据转换来的论坛)
            if (config.Passwordmode == 1)
            {
                //如果后台设置了必须使用安全提问
                if (config.Secques == 1)
                {
                    uid = Users.CheckDvBbsPasswordAndSecques(username, password, question, answer);
                }
                else
                {
                    uid = Users.CheckDvBbsPassword(username, password);
                }
            }
            //如果设置中Discuz!NT的密码模式为一般模式
            else
            {
                //如果后台设置了必须使用安全提问
                if (config.Secques == 1)
                {
                    uid = Users.CheckPasswordAndSecques(username, password, true, question, answer);
                }
                else
                {
                    //最典型的用户验证方法
                    uid = Users.CheckPassword(username, password, true);
                }
            }
            //如果CheckPassword方法的返回值不为-1,则代表已经找到了相应的合法用户
            if (uid != -1)
            {
                result.Add(true);
                ShortUserInfo __userinfo = Users.GetShortUserInfo(uid);
                if (__userinfo.Groupid == 8)
                {
                    //这里添加代码提示"抱歉, 您的用户身份尚未得到验证"并return退出
                    //...
                    //
                    if (config.Regverify == 1)
                    {
                        result.Add("这里添加代码提示“请您到您的邮箱中点击激活链接来激活您的帐号”并return退出");
                    }

                    if (config.Regverify == 2)
                    {
                        result.Add("这里添加代码提示“您需要等待一些时间, 待系统管理员审核您的帐户后才可登录使用”并return退出");
                    }
                }
                else
                {

                    //删除之前的错误登录信息
                    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));
                    //更新用户最后动作,如不需要可不执行
                    OnlineUsers.UpdateAction(olid, UserAction.Login.ActionID, 0, config.Onlinetimeout);
                    //更新该用户最后访问时间
                    Users.UpdateUserLastvisit(uid, DNTRequest.GetIP());

                    result.Add("登录成功");
                }

            }
            else
            {
                result.Add(false);
                int errcount = LoginLogs.UpdateLoginLog(DNTRequest.GetIP(), true);
                if (errcount > 5)
                {
                    result.Add("这里添加代码提示“您已经输入密码5次错误, 请15分钟后再试”并return退出");
                }
                else
                {
                    result.Add("密码或安全提问第" + errcount.ToString() + "次错误, 您最多有5次机会重试");
                }
            }
            return result;
        }

        public static void Logout()
        {
            Discuz.Config.GeneralConfigInfo config = Discuz.Config.GeneralConfigs.GetConfig();
            OnlineUserInfo oluserinfo = OnlineUsers.UpdateInfo(config.Passwordkey, config.Onlinetimeout);
            int olid = oluserinfo.Olid;

            int uid = Convert.ToInt32(ForumUtils.GetCookie("userid"));
            Users.UpdateOnlineTime(uid);
            OnlineUsers.DeleteRows(olid);
            ForumUtils.ClearUserCookie();
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="email"></param>
        /// <param name="question"></param>
        /// <param name="answer"></param>
        /// <returns></returns>
        public static bool Register(string username, string password, string email,int question,string answer)
        {
            GeneralConfigInfo config = GeneralConfigs.GetConfig();
            OnlineUserInfo oluserinfo = OnlineUsers.UpdateInfo(config.Passwordkey, config.Onlinetimeout);
            UserGroupInfo usergroupinfo = UserGroups.GetUserGroupInfo(oluserinfo.Groupid);

            //如果用户名符合注册规则, 则判断是否已存在
            if (Users.Exists(username))
            {
                //
                // 这里提示错误信息"用户名已经存在!"
                //
                return false;
            }

            UserInfo __userinfo = new UserInfo();
            __userinfo.Username = Utils.HtmlEncode(username);
            __userinfo.Nickname = "";
            __userinfo.Password = Utils.MD5(password);
            __userinfo.Secques = ForumUtils.GetUserSecques(question, answer);
            __userinfo.Gender = 0;
            __userinfo.Adminid = 0;
            __userinfo.Groupexpiry = 0;
            __userinfo.Extgroupids = "";
            __userinfo.Regip = DNTRequest.GetIP();
            __userinfo.Joindate = Utils.GetDateTime();
            __userinfo.Lastip = DNTRequest.GetIP();
            __userinfo.Lastvisit = Utils.GetDateTime();
            __userinfo.Lastactivity = Utils.GetDateTime();
            __userinfo.Lastpost = Utils.GetDateTime();
            __userinfo.Lastpostid = 0;
            __userinfo.Lastposttitle = "";
            __userinfo.Posts = 0;
            __userinfo.Digestposts = 0;
            __userinfo.Oltime = 0;
            __userinfo.Pageviews = 0;
            __userinfo.Credits = 0;
            __userinfo.Extcredits1 = Scoresets.GetScoreSet(1).Init;
            __userinfo.Extcredits2 = Scoresets.GetScoreSet(2).Init;
            __userinfo.Extcredits3 = Scoresets.GetScoreSet(3).Init;
            __userinfo.Extcredits4 = Scoresets.GetScoreSet(4).Init;
            __userinfo.Extcredits5 = Scoresets.GetScoreSet(5).Init;
            __userinfo.Extcredits6 = Scoresets.GetScoreSet(6).Init;
            __userinfo.Extcredits7 = Scoresets.GetScoreSet(7).Init;
            __userinfo.Extcredits8 = Scoresets.GetScoreSet(8).Init;
            __userinfo.Avatarshowid = 0;
            __userinfo.Email = email;
            __userinfo.Bday = tmpBday;
            __userinfo.Sigstatus = 1;

            if (__userinfo.Sigstatus != 0)
            {
                __userinfo.Sigstatus = 1;
            }
            __userinfo.Tpp = 0;
            __userinfo.Ppp = 0;
            __userinfo.Templateid = 1;
            __userinfo.Pmsound = 0;
            __userinfo.Showemail = 0;

            int receivepmsetting = 1;
            string receivesetting = "2,4";
            foreach (string rpms in receivesetting.Split(','))
            {
                if (rpms != string.Empty)
                {
                    int tmp = int.Parse(rpms);
                    receivepmsetting = receivepmsetting | tmp;
                }
            }

            if (config.Regadvance == 0)
            {
                receivepmsetting = 7;
            }

            __userinfo.Newsletter = (ReceivePMSettingType)receivepmsetting;
            __userinfo.Invisible = 0;
            __userinfo.Newpm = 0;
            __userinfo.Medals = "";
            if (config.Welcomemsg == 1)
            {
                __userinfo.Newpm = 1;
            }
            __userinfo.Accessmasks = 0;
            //
            __userinfo.Website = "";
            __userinfo.Icq = "";
            __userinfo.Qq = "";
            __userinfo.Yahoo = "";
            __userinfo.Msn = "";
            __userinfo.Skype = "";
            __userinfo.Location = "";
            if (usergroupinfo.Allowcstatus == 1)
            {
                __userinfo.Customstatus = "";
            }
            else
            {
                __userinfo.Customstatus = "";
            }
            __userinfo.Avatar = @"avatars\common\0.gif";
            __userinfo.Avatarwidth = 0;
            __userinfo.Avatarheight = 0;
            __userinfo.Bio = "";
            __userinfo.Signature = "";

            PostpramsInfo _postpramsinfo = new PostpramsInfo();
            _postpramsinfo.Usergroupid = usergroupid;
            _postpramsinfo.Attachimgpost = config.Attachimgpost;
            _postpramsinfo.Showattachmentpath = config.Showattachmentpath;
            _postpramsinfo.Hide = 0;
            _postpramsinfo.Price = 0;
            _postpramsinfo.Sdetail = __userinfo.Signature;
            _postpramsinfo.Smileyoff = 1;
            _postpramsinfo.Bbcodeoff = 1 - usergroupinfo.Allowsigbbcode;
            _postpramsinfo.Parseurloff = 1;
            _postpramsinfo.Showimages = usergroupinfo.Allowsigimgcode;
            _postpramsinfo.Allowhtml = 0;
            _postpramsinfo.Smiliesinfo = Smilies.GetSmiliesListWithInfo();
            _postpramsinfo.Customeditorbuttoninfo = Editors.GetCustomEditButtonListWithInfo();
            _postpramsinfo.Smiliesmax = config.Smiliesmax;

            __userinfo.Sightml = UBB.UBBToHTML(_postpramsinfo);

            //
            __userinfo.Authtime = Utils.GetDateTime();

            //邮箱激活链接验证
            if (config.Regverify == 1)
            {
                __userinfo.Authstr = ForumUtils.CreateAuthStr(20);
                __userinfo.Authflag = 1;
                __userinfo.Groupid = 8;
                SendEmail(tmpUsername, password.Trim(), email.Trim(), __userinfo.Authstr);
            }
            //系统管理员进行后台验证
            else if (config.Regverify == 2)
            {
                __userinfo.Authstr = "";
                __userinfo.Groupid = 8;
                __userinfo.Authflag = 1;
            }
            else
            {
                __userinfo.Authstr = "";
                __userinfo.Authflag = 0;
                __userinfo.Groupid = UserCredits.GetCreditsUserGroupID(0).Groupid;
            }
            __userinfo.Realname = "";
            __userinfo.Idcard = "";
            __userinfo.Mobile = "";
            __userinfo.Phone ="";

            int uid = Users.CreateUser(__userinfo);

            if (config.Welcomemsg == 1)
            {
                PrivateMessageInfo __privatemessageinfo = new PrivateMessageInfo();

                string curdatetime = Utils.GetDateTime();
                // 收件箱
                __privatemessageinfo.Message = config.Welcomemsgtxt;
                __privatemessageinfo.Subject = "欢迎您的加入! (请勿回复本信息)";
                __privatemessageinfo.Msgto = __userinfo.Username;
                __privatemessageinfo.Msgtoid = uid;
                __privatemessageinfo.Msgfrom = PrivateMessages.SystemUserName;
                __privatemessageinfo.Msgfromid = 0;
                __privatemessageinfo.New = 1;
                __privatemessageinfo.Postdatetime = curdatetime;
                __privatemessageinfo.Folder = 0;
                PrivateMessages.CreatePrivateMessage(__privatemessageinfo, 0);
            }

            if (config.Regverify == 0)
            {
                UserCredits.UpdateUserCredits(uid);
                ForumUtils.WriteUserCookie(uid, -1, config.Passwordkey);
                OnlineUsers.UpdateAction(olid, UserAction.Register.ActionID, 0, config.Onlinetimeout);


                Statistics.ReSetStatisticsCache();

                return true;
            }
            else
            {

                if (config.Regverify == 1)
                {
                    //这里添加代码提示“注册成功, 请您到您的邮箱中点击激活链接来激活您的帐号"并return退出
                    return true;
                }

                if (config.Regverify == 2)
                {
                    //这里添加代码提示“您注册成功, 但需要系统管理员审核您的帐户后才可登陆使用"并return退出
                    return true;
                }

            }

            return false;
        }

    }

 

 

3.如果想从www.domainname.xxx和bbs.domainname.XXX同步还得设置一下config\general.config
将 <CookieDomain/>改为<CookieDomain>domainname.XXX</CookieDomain>

 

你可能感兴趣的:(asp.net)