星期零

技术改变生活,分享让我们快乐!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

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();
        }

就彻底解决了这个问题。