【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 4200|回复: 1

其他语言整合UC流程说明(附 c#版本的authcode() 函数的代码 )

[复制链接]
发表于 2013-1-22 16:44:06 | 显示全部楼层 |阅读模式
其他语言开发UC接口流程说明(附 c#版本的authcode() 函数的代码 )

因为时间关系,我们只推出了PHP版本的UC,在这里我简要给大家说明下其他语言整合UC的流程。

关于UC的工作原理我再简要说明下:
UC相当于一个WEB SERVICE,所有应用(指你的站内的应用)都会来这里登记自己的信息。它起的作用就是一个中心枢纽,可以认为它是一个中转中心,或者是公用数据中心。

在介绍流程时,我将几个关键点说明下:
1. UC 给每个应用分配一个唯一ID,我们称之为 appid。
2. UC 给每个应用分配一个密钥,用来加密/解密数据,防止被窃听,加密函数 authcode()。
3. UC 跟应用通讯的协议为 HTTP,在 PHP 版本的 UC 中,我们实现了非 HTTP 方式,其他语言不用关心这个。
4. 应用发送给 UC 的为 GET/POST 数据,格式与普通表单类似,如 http://server.uc.com/index.php?m=user&a=login
5. UC 回应的数据为 XML 格式,后面我们会详细说明。
6. 本教程以PHP代码为例,供其他语言参考。
7. 假定如下部署:
        UC URL: http://uc.domain.com/
        应用 URL: http://bbs.domain.com/
        接受 UC 通知的地址: http://bbs.domain.com/api/uc.aspx

        

下面我来说一下具体流程:

第一步:

登录 http://uc.domain.com/ 进入“应用管理” => “添加应用” => 选择自定义安装

通信密钥随便填写几个字符,假定填写的为: abcdefgh。
应用名称: Demo BBS
接口URL : http://bbs.domain.com/
应用IP留空
应用类型: 其他
同步登陆: 是
是否接受通知: 否
标签模板那两条留空。

提交后,提示:成功添加应用。ID:2

第二步:

根据第一个的结果,我们手工编写如下配置文件,保存为 http://bbs.domain.com/uc.config.php


  1. <?php

  2. define('UC_API', 'http://bbs.domain.com/');                // UCenter 的 URL 地址
  3. define('UC_IP', '');                                        // 避免每次都尝试DNS解析,最好填上
  4. define('UC_APPID', '2');                                // 当前应用的 ID
  5. define('UC_KEY', 'abcdefgh');                                // 与 UCenter 的通信密钥, 要与 UCenter 保持一致

  6. ?>
复制代码
第三步:

发送数据给 UC,比如我们要调用 UC 注册用户的接口。

建立 http://bbs.domain.com/test_uc_reg.php 文件


  1. <?php

  2. require '../uc.config.php';

  3. $username = 'abc';
  4. $password = '123';
  5. $email = 'aaa@aaa.com';

  6. // 构造原始的POST数据
  7. $postdata = 'username=abc&password=123&email=aaa@aaa.com';

  8. // 数据加密,加密函数 uc_authcode() ,附件中包含 c# 的实现
  9. $postdata = urlencode(uc_authcode($postdata.'&agent='.md5($_SERVER['HTTP_USER_AGENT'])."&time=".time(), 'ENCODE', UC_KEY));

  10. // 指明请求的模块和动作,m=模块 a=动作 inajax=2表示返回的是XML格式 input=表示加密后的数据 appid=应用的ID标示
  11. // 模块,动作,参数,可以参考 UCenter 1.0.0 接口开发手册
  12. $postdata = "m=user&a=register&inajax=2&input=$postdata&appid=".UC_APPID;

  13. // 将数据POST方式发送到 UC $result 为 UC 返回的数据。
  14. $result = uc_fopen(UC_API.'/index.php', 500000, 'username=abc&password=123&email=aaa@aaa.com', '', TRUE, UC_IP, 20);

  15. // 返回的结果,可以参考 UCenter 1.0.0 接口开发手册
  16. if($result == '-1') {
  17.         // 用户名不合法        
  18. } elseif($result == '-2') {
  19.         // 包含不允许注册的词语
  20. } elseif($result == '-3') {
  21.         // 用户名已经存在
  22. } elseif($result == '-4') {
  23.         // email 格式有误
  24. } elseif($result == '-5') {
  25.         // email 不允许注册
  26. } elseif($result == '-6') {
  27.         // 该 email 已经被注册
  28. } else {
  29.         // 注册成功, uid 为 $result
  30.         $uid = $result;
  31. }

  32. function uc_fopen($url, $limit = 0, $post = '', $cookie = '', $bysocket = FALSE, $ip = '', $timeout = 15, $block = TRUE) {
  33.         $return = '';
  34.         $matches = parse_url($url);
  35.         !isset($matches['host']) && $matches['host'] = '';
  36.         !isset($matches['path']) && $matches['path'] = '';
  37.         !isset($matches['query']) && $matches['query'] = '';
  38.         !isset($matches['port']) && $matches['port'] = '';
  39.         $host = $matches['host'];
  40.         $path = $matches['path'] ? $matches['path'].($matches['query'] ? '?'.$matches['query'] : '') : '/';
  41.         $port = !empty($matches['port']) ? $matches['port'] : 80;
  42.         if($post) {
  43.                 $out = "POST $path HTTP/1.0\r\n";
  44.                 $out .= "Accept: */*\r\n";
  45.                 //$out .= "Referer: $boardurl\r\n";
  46.                 $out .= "Accept-Language: zh-cn\r\n";
  47.                 $out .= "Content-Type: application/x-www-form-urlencoded\r\n";
  48.                 $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  49.                 $out .= "Host: $host\r\n";
  50.                 $out .= 'Content-Length: '.strlen($post)."\r\n";
  51.                 $out .= "Connection: Close\r\n";
  52.                 $out .= "Cache-Control: no-cache\r\n";
  53.                 $out .= "Cookie: $cookie\r\n\r\n";
  54.                 $out .= $post;
  55.         } else {
  56.                 $out = "GET $path HTTP/1.0\r\n";
  57.                 $out .= "Accept: */*\r\n";
  58.                 //$out .= "Referer: $boardurl\r\n";
  59.                 $out .= "Accept-Language: zh-cn\r\n";
  60.                 $out .= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
  61.                 $out .= "Host: $host\r\n";
  62.                 $out .= "Connection: Close\r\n";
  63.                 $out .= "Cookie: $cookie\r\n\r\n";
  64.         }
  65.         $fp = @fsockopen(($ip ? $ip : $host), $port, $errno, $errstr, $timeout);
  66.         if(!$fp) {
  67.                 return '';//note $errstr : $errno \r\n
  68.         } else {
  69.                 stream_set_blocking($fp, $block);
  70.                 stream_set_timeout($fp, $timeout);
  71.                 @fwrite($fp, $out);
  72.                 $status = stream_get_meta_data($fp);
  73.                 if(!$status['timed_out']) {
  74.                         while (!feof($fp)) {
  75.                                 if(($header = @fgets($fp)) && ($header == "\r\n" ||  $header == "\n")) {
  76.                                         break;
  77.                                 }
  78.                         }

  79.                         $stop = false;
  80.                         while(!feof($fp) && !$stop) {
  81.                                 $data = fread($fp, ($limit == 0 || $limit > 8192 ? 8192 : $limit));
  82.                                 $return .= $data;
  83.                                 if($limit) {
  84.                                         $limit -= strlen($data);
  85.                                         $stop = $limit <= 0;
  86.                                 }
  87.                         }
  88.                 }
  89.                 @fclose($fp);
  90.                 return $return;
  91.         }
  92. }


  93. /**
  94. * 字符串加密以及解密函数
  95. *
  96. * @param string $string        原文或者密文
  97. * @param string $operation        操作(ENCODE | DECODE), 默认为 DECODE
  98. * @param string $key                密钥
  99. * @param int $expiry                密文有效期, 加密时候有效, 单位 秒,0 为永久有效
  100. * @return string                处理后的 原文或者 经过 base64_encode 处理后的密文
  101. *
  102. * @example
  103. *
  104. *         $a = authcode('abc', 'ENCODE', 'key');
  105. *         $b = authcode($a, 'DECODE', 'key');  // $b(abc)
  106. *
  107. *         $a = authcode('abc', 'ENCODE', 'key', 3600);
  108. *         $b = authcode('abc', 'DECODE', 'key'); // 在一个小时内,$b(abc),否则 $b 为空
  109. */
  110. function uc_authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {

  111.         $ckey_length = 4;        //note 随机密钥长度 取值 0-32;
  112.                                 //note 加入随机密钥,可以令密文无任何规律,即便是原文和密钥完全相同,加密结果也会每次不同,增大破解难度。
  113.                                 //note 取值越大,密文变动规律越大,密文变化 = 16 的 $ckey_length 次方
  114.                                 //note 当此值为 0 时,则不产生随机密钥

  115.         $key = md5($key ? $key : UC_KEY);
  116.         $keya = md5(substr($key, 0, 16));
  117.         $keyb = md5(substr($key, 16, 16));
  118.         $keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';

  119.         $cryptkey = $keya.md5($keya.$keyc);
  120.         $key_length = strlen($cryptkey);

  121.         $string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
  122.         $string_length = strlen($string);

  123.         $result = '';
  124.         $box = range(0, 255);

  125.         $rndkey = array();
  126.         for($i = 0; $i <= 255; $i++) {
  127.                 $rndkey[$i] = ord($cryptkey[$i % $key_length]);
  128.         }

  129.         for($j = $i = 0; $i < 256; $i++) {
  130.                 $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  131.                 $tmp = $box[$i];
  132.                 $box[$i] = $box[$j];
  133.                 $box[$j] = $tmp;
  134.         }

  135.         for($a = $j = $i = 0; $i < $string_length; $i++) {
  136.                 $a = ($a + 1) % 256;
  137.                 $j = ($j + $box[$a]) % 256;
  138.                 $tmp = $box[$a];
  139.                 $box[$a] = $box[$j];
  140.                 $box[$j] = $tmp;
  141.                 $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  142.         }

  143.         if($operation == 'DECODE') {
  144.                 if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
  145.                         return substr($result, 26);
  146.                 } else {
  147.                         return '';
  148.                 }
  149.         } else {
  150.                 return $keyc.str_replace('=', '', base64_encode($result));
  151.         }
  152. }
复制代码
金山快盘附件DiscuzAuthcode.rar(11.48KB)
金山快盘附件其他语言开发UC接口流程说明(附 c#版本的authcode() 函数的代码 ).txt(7.77KB)
发表于 2013-6-6 10:47:35 | 显示全部楼层
绝对喜欢,好帖子不多哦,顶一下

qq美女皮肤 情侣签名 霸气情侣网名 非主流带字男生头像 非主流图片唯美 
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2025-2-6 04:04

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表