先看一下效果
一 首先准备材料:
1.ucenter for net : or
2.uc.ashx 下面是具体代码
using DS.Web.UCenter;using DS.Web.UCenter.Api;using DS.Web.UCenter.Client;using log4net;using pc_manishi.Controllers;using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace pc_manishi.API{ ////// Summary description for uc /// public class uc : UcApiBase { HttpResponse Response = HttpContext.Current.Response; HttpRequest Request = HttpContext.Current.Request; private static readonly ILog logger = LogManager.GetLogger(typeof(AddressController)); public override ApiReturn DeleteUser(IEnumerable ids) { throw new NotImplementedException(); } public override ApiReturn RenameUser(int uid, string oldUserName, string newUserName) { throw new NotImplementedException(); } public override UcTagReturns GetTag(string tagName) { throw new NotImplementedException(); } //登陆 public override ApiReturn SynLogin(int uid) { try { IUcClient client = new UcClient(); UcUserInfo user = client.UserInfo(uid); if (user.Success) { //LoggerCore.Debug(user.Uid + "--" + user.UserName); HttpContext.Current.Session["uid"] = user.Uid; HttpContext.Current.Session["username"] = user.UserName; HttpContext.Current.Session["email"] = user.Mail; return ApiReturn.Success; } return ApiReturn.Failed; } catch (Exception ex) { logger.Error("远程登录错误", ex); return ApiReturn.Failed; } //throw new NotImplementedException(); } //登出 public override ApiReturn SynLogout() { try { logger.Error("论坛正在登出操作"); //HttpContext.Current.Session.Abandon(); } catch (Exception ex) { logger.Error("远程退出错误", ex); } return ApiReturn.Success; //throw new NotImplementedException(); } public override ApiReturn UpdatePw(string userName, string passWord) { throw new NotImplementedException(); } public override ApiReturn UpdateBadWords(UcBadWords badWords) { throw new NotImplementedException(); } public override ApiReturn UpdateHosts(UcHosts hosts) { throw new NotImplementedException(); } public override ApiReturn UpdateApps(UcApps apps) { throw new NotImplementedException(); } public override ApiReturn UpdateClient(UcClientSetting client) { throw new NotImplementedException(); } public override ApiReturn UpdateCredit(int uid, int credit, int amount) { throw new NotImplementedException(); } public override UcCreditSettingReturns GetCreditSettings() { throw new NotImplementedException(); } public override ApiReturn GetCredit(int uid, int credit) { throw new NotImplementedException(); } public override ApiReturn UpdateCreditSettings(UcCreditSettings creditSettings) { throw new NotImplementedException(); } }}
二 正式开始:
首先在自己的mvc项目里面简历api 文件夹,把创建uc.ashx 文件
创建ashx文件的时候一定要注意命名空间,生成的跟现在项目的不一样但编译通过,我的因为这个调试了很长时间才发现这个地方错了
接着配置webconfig 文件,添加如下配置(配置根据自己的情况而定)
UC_KEY,UC_API,UC_APPID 其中这3个必填,一定要改成自己添加的net网站的配置,我这里如下图配置 接下来就期待通信成功了,如果不成功,那就检查discuz里 config_ucenter.php 跟自己网站的webconfig的配置 Discuz!Board 一般会通信成功的,因为我在安装discuz的时候也一起安装了ucenter除非你对网站搬家了,以后说搬家后的问题 接下来就是同步登陆了: 这里是我的代码 其实就是调用了一下 IUcClient类的方法,然后通过usersynlogin来拿到远程登陆的js代码,我这里不知道为什么,用 Response.Write() 来输出到前端,但我这里返回到了xmlHttpResponse里面,html里面拿不到 所以我这里用webhttprequest进行了一部处理,原理就是返回的js里面有个src,你直接在浏览器访问一下就把要登陆的cookie下来了,但要注意cookie的域名要设置二级域名共享,退出的话也是同样的处理 再说一下注册:注册这里我关闭了验证码,去掉了邮箱必填(度娘可以告诉你怎么关闭) 下面也直接调用 client.UserRegister(username, Password, string.Format("{0:ddhhmmss}", DateTime.Now) + "@suiji.com", 0, ""); 及时你去掉了邮箱必填也要给一个默认邮箱,这里我随机生成了一个邮箱 还有一点要注意,即使你开启了立即激活 也需要登陆一下让discuz自动激活你的用户 下面再说一下搬家的注意事项(我这里从winserver 搬到了linux上): 1:配置里的url cookie的域名 这些都要更改 2.在用ftp修改的时候一定不要使用notepad,这样会产生编码问题,导致通信失败 3.data source template 等这几个文件夹及文件的777权限(当你出问题是不妨检查一下文件权限) 文章写的不是很详细,大家多多见谅 感谢: