zoukankan      html  css  js  c++  java
  • 网站整合Discuz!NT论坛,网站用户和论坛用户统一并同步,详解(二)

    网站会员注册时同步会员信息到论坛:

    try
    {
        string cookieDomain = ".bubufx.com";//你的域名
        DiscuzSession ds = WebSampleHelper.DiscuzSessionHelper.GetSession();
        Discuz.Toolkit.User user = null;
        if (user == null || user.UserName != t_member_loginName)
        {
            int uid = ds.GetUserID(t_member_loginName);
            if (uid < 1)
            {
                uid = ds.Register(t_member_loginName, FormsAuthentication.HashPasswordForStoringInConfigFile(t_member_loginPass, "MD5").ToLower(), t_member_email, true);            
                            
            }
            else
                ds.Login(uid, t_member_loginPass, true, 10000, cookieDomain);
        }
    }
    catch { }

    同步密码修改:

    try
    {
        DiscuzSession ds = WebSampleHelper.DiscuzSessionHelper.GetSession();
        Discuz.Toolkit.User user = null;
        if (user == null || user.UserName != cm.memberLoginname)
        {
            int uid = ds.GetUserID(cm.memberLoginname);
            if (uid > 0)
            {
                ds.ChangeUserPassword(uid, loginpassOld, loginpass, loginpass, "");
            }
        }
    }
    catch { }

    注册用户时,会碰到以下异常:当前会话所提交的call_id没有大于前一次的call_id

    出现这个错误的问题就是DiscuzToolkit线程同步问题,这个问题是由Discuz.Toolkit.Util的Sign方法引起的,在这里,它为每一个API请求生成一个call_id。

    list.Add(DiscuzParam.Create("call_id", DateTime.Now.Ticks));

    做如下修改可以彻底解决这个问题:

    将原来DiscuzToolkit项目里的Util.cs文件中的如下代码:

    public static XmlSerializer GetSerializer(Type t)
            {
                int type_hash = t.GetHashCode();
    
                if (!serializer_dict.ContainsKey(type_hash))
                    serializer_dict.Add(type_hash, new XmlSerializer(t));
    
                return serializer_dict[type_hash];
            }
    
            public DiscuzParam[] Sign(string method_name, DiscuzParam[] parameters)
            {
                List<DiscuzParam> list = new List<DiscuzParam>(parameters);
                list.Add(DiscuzParam.Create("method", method_name));
                list.Add(DiscuzParam.Create("api_key", api_key));
                list.Add(DiscuzParam.Create("call_id", DateTime.Now.Ticks));
                list.Sort();
    
                StringBuilder values = new StringBuilder();
    
                foreach (DiscuzParam param in list)
                {
                    if (!string.IsNullOrEmpty(param.Value))
                        values.Append(param.ToString());
                }
    
                values.Append(secret);
    
                byte[] md5_result = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(values.ToString()));
    
                StringBuilder sig_builder = new StringBuilder();
    
                foreach (byte b in md5_result)
                    sig_builder.Append(b.ToString("x2"));
    
                list.Add(DiscuzParam.Create("sig", sig_builder.ToString()));
    
                return list.ToArray();
            }

    更改为:

    public static XmlSerializer GetSerializer(Type t)
            {
                int type_hash = t.GetHashCode();
    
                if (!serializer_dict.ContainsKey(type_hash))
                    serializer_dict.Add(type_hash, new XmlSerializer(t));
    
                return serializer_dict[type_hash];
            }
    
            public DiscuzParam[] Sign(string method_name, DiscuzParam[] parameters)
            {
                List<DiscuzParam> list = new List<DiscuzParam>(parameters);
                list.Add(DiscuzParam.Create("method", method_name));
                list.Add(DiscuzParam.Create("api_key", api_key));
    
                //list.Add(DiscuzParam.Create("call_id", DateTime.Now.Ticks));
                //
                lock (this)
                {
                    list.Add(DiscuzParam.Create("call_id", DateTime.Now.Ticks));
                    // Avoid to generate same ’call_id’ and throws an exception on ’当前会话所提交的call_id没有大于前一次的call_id’. 
                    System.Threading.Thread.Sleep(50);
                }
                //
    
                list.Sort();
    
                StringBuilder values = new StringBuilder();
    
                foreach (DiscuzParam param in list)
                {
                    if (!string.IsNullOrEmpty(param.Value))
                        values.Append(param.ToString());
                }
    
                values.Append(secret);
    
                byte[] md5_result = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(values.ToString()));
    
                StringBuilder sig_builder = new StringBuilder();
    
                foreach (byte b in md5_result)
                    sig_builder.Append(b.ToString("x2"));
    
                list.Add(DiscuzParam.Create("sig", sig_builder.ToString()));
    
                return list.ToArray();
            }

    就彻底解决了这个问题。

  • 相关阅读:
    linq to entity之字符串比较
    linq to entity 分页方法不支持LastOrDefault等操作的处理办法
    easyui蛋疼之二 tabs与accordion
    根据下拉框生成控件列表
    第一个flash AS代码
    250多个Jquery各式各样的插件
    对用户控件的访问方式重写
    WP7之Slider绑定textblock设置字体大小
    辉南三角龙湾
    回到正轨
  • 原文地址:https://www.cnblogs.com/weekzero/p/3043772.html
Copyright © 2011-2022 走看看