先看一下效果php
1.ucenter for net :https://github.com/dozer47528/UCenter-API-For-DotNet or http://pan.baidu.com/s/1i3znQV7html
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> /// Summary description for uc /// </summary> 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<int> 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 文件 linux
建立ashx文件的時候必定要注意命名空間,生成的跟如今項目的不同但編譯經過,個人由於這個調試了很長時間才發現這個地方錯了git
接着配置webconfig 文件,添加以下配置(配置根據本身的狀況而定)github
<!--客戶端版本--> <add key="UC_CLIENT_VERSION" value="1.6.0"/> <!--發行時間--> <add key="UC_CLIENT_RELEASE" value="20141101"/> <!--API 開關(value類型:True False 默認值:True)--> <!--是否容許刪除用戶--> <add key="API_DELETEUSER" value="True"/> <!--是否容許重命名用戶--> <add key="API_RENAMEUSER" value="True"/> <!--是否容許獲得標籤--> <add key="API_GETTAG" value="True"/> <!--是否容許同步登陸--> <add key="API_SYNLOGIN" value="True"/> <!--是否容許同步登出--> <add key="API_SYNLOGOUT" value="True"/> <!--是否容許更改密碼--> <add key="API_UPDATEPW" value="True"/> <!--是否容許更新關鍵字--> <add key="API_UPDATEBADWORDS" value="True"/> <!--是否容許更新域名解析緩存--> <add key="API_UPDATEHOSTS" value="True"/> <!--是否容許更新應用列表--> <add key="API_UPDATEAPPS" value="True"/> <!--是否容許更新客戶端緩存--> <add key="API_UPDATECLIENT" value="True"/> <!--是否容許更新用戶積分--> <add key="API_UPDATECREDIT" value="True"/> <!--是否容許向UCenter提供積分設置--> <add key="API_GETCREDITSETTINGS" value="True"/> <!--是否容許獲取用戶的某項積分--> <add key="API_GETCREDIT" value="True"/> <!--是否容許更新應用積分設置--> <add key="API_UPDATECREDITSETTINGS" value="True"/> <!--API 開關結束--> <!--返回值設置--> <!--返回成功(默認:1)--> <add key="API_RETURN_SUCCEED" value="1"/> <!--返回失敗(默認:-1)--> <add key="API_RETURN_FAILED" value="-1"/> <!--返回禁用(默認:-2)--> <add key="API_RETURN_FORBIDDEN" value="-2"/> <!--返回值設置結束--> <!--[必填]通訊密鑰--> <add key="UC_KEY" value="20150612"/> <!--[必填]UCenter地址--> <add key="UC_API" value="http://XXX/uc_server"/> <!--[必填]默認編碼--> <add key="UC_CHARSET" value="utf-8"/> <!--[非必填]UCenter IP--> <add key="UC_IP" value=""/> <!--[必填]應用ID--> <add key="UC_APPID" value="2"/>
UC_KEY,UC_API,UC_APPID 其中這3個必填,必定要改爲本身添加的net網站的配置,我這裏以下圖配置
![](http://static.javashuo.com/static/loading.gif)
接下來就期待通訊成功了,若是不成功,那就檢查discuz裏 config_ucenter.php 跟本身網站的webconfig的配置
Discuz!Board 通常會通訊成功的,由於我在安裝discuz的時候也一塊兒安裝了ucenter除非你對網站搬家了,之後說搬家後的問題
接下來就是同步登錄了:
這裏是個人代碼
![](http://static.javashuo.com/static/loading.gif)
其實就是調用了一下 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, "");
及時你去掉了郵箱必填也要給一個默認郵箱,這裏我隨機生成了一個郵箱
還有一點要注意,即便你開啓了當即激活![](http://static.javashuo.com/static/loading.gif)
也須要登錄一下讓discuz自動激活你的用戶
下面再說一下搬家的注意事項(我這裏從winserver 搬到了linux上):
1:配置裏的url cookie的域名 這些都要更改
2.在用ftp修改的時候必定不要使用notepad,這樣會產生編碼問題,致使通訊失敗
3.data source template 等這幾個文件夾及文件的777權限(當你出問題是不妨檢查一下文件權限)
![](http://static.javashuo.com/static/loading.gif)
文章寫的不是很詳細,你們多多見諒
感謝:
UCenter API For .Net 在 CodePlex 上發佈啦!公司裏的php,linux同事