Use UCenter to achieve simultaneous logging in and out of discuz Forum and application website function test environment: DISCUZ! X3.2, UCenter 1.6,.net Framework 4.0

The UCenter application management page is displayed on the Discuz background

Adding a New app

Set installation mode to custom installation and application type to other. The communication key can be customized. Enable synchronous login and notification

Integrate UCenter API For DotNet

Modify the configuration information of the project app. Config. UC_KEY Communication key entered when adding a new application UC_API UCenter ADDRESS UC_APPID ID of the application website in UCenter

Run the site after configuration and refresh the app list interface

If the configuration information is correct, the application communication succeeds.

Call UCenter API

Taking login as an example, instantiate a UcClient object and invoke the UserLogin method

IUcClient client = new UcClient();
var user = client.UserLogin("admin"."admin"); / / login
if (user.Success) // Check whether the login is successful
{
    client.PmSend(0.0."Notice"."Test Bulletin", user.Uid); // Send a system message to the user
}
Copy the code

Other related apis

using System.Collections.Generic;

namespace DS.Web.UCenter.Client
{
    ///<summary>
    ///UcApi client
    ///</summary>
    public interface IUcClient
    {
        /// <summary>
        ///User registration
        /// </summary>
        /// <param name="userName">The user name</param>
        /// <param name="passWord">password</param>
        /// <param name="email">Email</param>
        /// <param name="questionId">Log in problems</param>
        /// <param name="answer">The answer</param>
        /// <returns></returns>
        UcUserRegister UserRegister(string userName, string passWord, string email, int questionId = 0.string answer = "");

        /// <summary>
        ///The user login
        /// </summary>
        /// <param name="userName">Uid/user name/Email</param>
        /// <param name="passWord">password</param>
        /// <param name="loginMethod">Log on to way</param>
        /// <param name="checkques">Login problem</param>
        /// <param name="questionId">Question ID</param>
        /// <param name="answer">The answer</param>
        /// <returns></returns>
        UcUserLogin UserLogin(string userName, string passWord, LoginMethod loginMethod = LoginMethod.UserName, bool checkques = false.int questionId = 0.string answer = "");

        /// <summary>
        ///Get user information
        /// </summary>
        /// <param name="userName">The user name</param>
        /// <returns></returns>
        UcUserInfo UserInfo(string userName);

        /// <summary>
        ///Get user information
        /// </summary>
        /// <param name="uid">Uid</param>
        /// <returns></returns>
        UcUserInfo UserInfo(int uid);

        /// <summary>
        ///Updating User Information
        ///To update information, verify that the old password is correct unless ignoreoldpw is set to 1.
        ///If only the Email address is changed but the password is not changed, leave newPW empty.
        ///If you change only the password but not the Email address, leave the Email address empty.
        /// </summary>
        /// <returns></returns>
        UcUserEdit UserEdit(string userName, string oldPw, string newPw, string email, bool ignoreOldPw = false.int questionId = 0.string answer = "");

        /// <summary>
        ///Delete user
        /// </summary>
        /// <param name="uid">Uid</param>
        /// <returns></returns>
        bool UserDelete(params int[] uid);

        /// <summary>
        ///Deleting a User profile Picture
        /// </summary>
        /// <param name="uid">Uid</param>
        void UserDeleteAvatar(params int[] uid);

        /// <summary>
        ///Synchronous landing
        /// </summary>
        /// <param name="uid">Uid</param>
        /// <returns>Synchronized login Html code</returns>
        string UserSynlogin(int uid);

        /// <summary>
        ///Synchronous logout
        /// </summary>
        /// <returns>Synchronous logout of THE Html code</returns>
        string UserSynLogout();

        /// <summary>
        ///Checking Email Format
        /// </summary>
        /// <param name="email">Email</param>
        /// <returns></returns>
        UcUserCheckEmail UserCheckEmail(string email);

        /// <summary>
        ///Example Add a protected user
        /// </summary>
        /// <param name="admin">Operation administrator</param>
        /// <param name="userName">The user name</param>
        /// <returns></returns>
        bool UserAddProtected(string admin, params string[] userName);

        /// <summary>
        ///Example Delete a protected user
        /// </summary>
        /// <param name="admin">Operation administrator</param>
        /// <param name="userName">The user name</param>
        /// <returns></returns>
        bool UserDeleteProtected(string admin, params string[] userName);

        /// <summary>
        ///Get the protected user
        /// </summary>
        /// <returns></returns>
        UcUserProtecteds UserGetProtected();

        /// <summary>
        ///Merge user
        /// </summary>
        /// <param name="oldUserName">Old user name</param>
        /// <param name="newUserName">A new user name</param>
        /// <param name="uid">Uid</param>
        /// <param name="passWord">password</param>
        /// <param name="email">Email</param>
        /// <returns></returns>
        UcUserMerge UserMerge(string oldUserName, string newUserName, int uid, string passWord, string email);

        /// <summary>
        ///Example Remove a user with the same name
        /// </summary>
        /// <param name="userName">The user name</param>
        void UserMergeRemove(string userName);

        /// <summary>
        ///Get user points
        /// </summary>
        /// <param name="appId">Application Id</param>
        /// <param name="uid">Uid</param>
        /// <param name="credit">Integral number</param>
        /// <returns></returns>
        int UserGetCredit(int appId, int uid, int credit);

        /// <summary>
        ///Checking for new messages
        /// </summary>
        /// <param name="uid">Uid</param>
        /// <returns></returns>
        UcPmCheckNew PmCheckNew(int uid);

        /// <summary>
        ///Sending short messages
        /// </summary>
        /// <param name="fromUid">Sender USER ID. 0 indicates a system message</param>
        /// <param name="replyPmId">Reply MESSAGE ID. 0: sends a new short message. Greater than 0: replies a specified short message</param>
        /// <param name="subject">News headlines</param>
        /// <param name="message">The message content</param>
        /// <param name="msgTo">The recipient ID</param>
        /// <returns></returns>
        UcPmSend PmSend(int fromUid, int replyPmId, string subject, string message, params int[] msgTo);

        /// <summary>
        ///Sending short messages
        /// </summary>
        /// <param name="fromUid">Sender USER ID. 0 indicates a system message</param>
        /// <param name="replyPmId">Reply MESSAGE ID. 0: sends a new short message. Greater than 0: replies a specified short message</param>
        /// <param name="subject">News headlines</param>
        /// <param name="message">The message content</param>
        /// <param name="msgTo">Recipient User Name</param>
        /// <returns></returns>
        UcPmSend PmSend(int fromUid, int replyPmId, string subject, string message, params string[] msgTo);

        /// <summary>
        ///Deleting short Messages
        /// </summary>
        /// <param name="uid">Uid</param>
        /// <param name="folder">folder</param>
        /// <param name="pmIds">Short message ID</param>
        /// <returns>Deletions</returns>
        int PmDelete(int uid, PmDeleteFolder folder, params int[] pmIds);

        /// <summary>
        ///Delete the session
        /// </summary>
        /// <param name="uid">The sender</param>
        /// <param name="toUids">The recipient</param>
        /// <returns>Deletions</returns>
        int PmDelete(int uid, params int[] toUids);

        /// <summary>
        ///Modify reading Status
        /// </summary>
        /// <param name="uid">The sender</param>
        /// <param name="toUids">The recipient</param>
        /// <param name="pmIds">Short message ID</param>
        /// <param name="readStatus">Reading state</param>
        void PmReadStatus(int uid, int toUids, int pmIds = 0, ReadStatus readStatus = ReadStatus.Readed);

        /// <summary>
        ///Modify reading Status
        /// </summary>
        /// <param name="uid">The sender</param>
        /// <param name="toUids">Recipient array</param>
        /// <param name="pmIds">Array of short message ids</param>
        /// <param name="readStatus">Reading state</param>
        void PmReadStatus(int uid, IEnumerable<int> toUids, IEnumerable<int> pmIds, ReadStatus readStatus = ReadStatus.Readed);

        /// <summary>
        ///Gets the short message list
        /// </summary>
        /// <param name="uid">Uid</param>
        /// <param name="page">Current page number. Default value: 1</param>
        /// <param name="pageSize">Maximum number of entries per page. Default value: 10</param>
        /// <param name="folder">Folder where the short message resides</param>
        /// <param name="filter">Filtering way</param>
        /// <param name="msgLen">Length of text to intercept short message content. 0 indicates that the text is not intercepted. The default value is 0</param>
        /// <returns></returns>
        UcPmList PmList(int uid, int page = 1.int pageSize = 10, PmReadFolder folder = PmReadFolder.NewBox, PmReadFilter filter = PmReadFilter.NewPm, int msgLen = 0);

        /// <summary>
        ///Gets the short message content
        ///This interface function returns a message with the specified message ID for the specified user. The returned data contains the reply to the message.
        ///If the touID parameter is specified, the short message lists all short messages between uIds and touids, and the Daterange can specify a range of dates to return the message.
        /// </summary>
        /// <param name="uid">Uid</param>
        /// <param name="pmId">Short message ID</param>
        /// <param name="toUid">The recipient ID</param>
        /// <param name="dateRange">Date range</param>
        /// <returns></returns>
        UcPmView PmView(int uid, int pmId, int toUid = 0, DateRange dateRange = DateRange.Today);

        /// <summary>
        ///Gets the content of a single short message
        /// </summary>
        /// <param name="uid">Uid</param>
        /// <param name="type">type</param>
        /// <param name="pmId">Short message ID</param>
        /// <returns></returns>
        UcPm PmViewNode(int uid, ViewType type = ViewType.Specified, int pmId = 0);

        /// <summary>
        ///Ignore unread message prompt
        /// </summary>
        /// <param name="uid">Uid</param>
        void PmIgnore(int uid);

        /// <summary>
        ///Get blacklisted
        /// </summary>
        /// <param name="uid">Uid</param>
        /// <returns></returns>
        UcPmBlacklsGet PmBlacklsGet(int uid);

        /// <summary>
        ///Set blacklist to prohibit all (clear raw data)
        /// </summary>
        /// <param name="uid">Uid</param>
        /// <returns></returns>
        bool PmBlacklsSetAll(int uid);

        /// <summary>
        ///Setting a blacklist (Clearing raw data)
        /// </summary>
        /// <param name="uid">Uid</param>
        /// <param name="userName">Blacklist User Name</param>
        /// <returns></returns>
        bool PmBlacklsSet(int uid, params string[] userName);

        /// <summary>
        ///Add blacklist to forbid all
        /// </summary>
        /// <param name="uid">Uid</param>
        /// <returns></returns>
        bool PmBlacklsAddAll(int uid);

        /// <summary>
        ///Add a blacklist
        /// </summary>
        /// <param name="uid">Uid</param>
        /// <param name="userName">Blacklist User Name</param>
        /// <returns></returns>
        bool PmBlacklsAdd(int uid, params string[] userName);

        /// <summary>
        ///Delete all prohibited persons in the blacklist
        /// </summary>
        /// <param name="uid">Uid</param>
        /// <returns></returns>
        void PmBlacklsDeleteAll(int uid);

        /// <summary>
        ///Deleting a Blacklist
        /// </summary>
        /// <param name="uid">Uid</param>
        /// <param name="userName">Blacklist User Name</param>
        void PmBlacklsDelete(int uid, params string[] userName);

        /// <summary>
        ///Added friend
        /// </summary>
        /// <param name="uid">Uid</param>
        /// <param name="friendId">Friend ID</param>
        /// <param name="comment">note</param>
        /// <returns></returns>
        bool UcFriendAdd(int uid, int friendId, string comment = "");

        /// <summary>
        ///Remove buddy
        /// </summary>
        /// <param name="uid">Uid</param>
        /// <param name="friendIds">Friend ID</param>
        /// <returns></returns>
        bool UcFriendDelete(int uid, params int[] friendIds);

        /// <summary>
        ///Get total number of friends
        /// </summary>
        /// <param name="uid">Uid</param>
        /// <param name="direction">The direction of</param>
        /// <returns>Friends number</returns>
        int UcFriendTotalNum(int uid, FriendDirection direction = FriendDirection.All);

        /// <summary>
        ///Friends list
        /// </summary>
        /// <param name="uid">Uid</param>
        /// <param name="page">Current page Number</param>
        /// <param name="pageSize">Maximum number of entries per page</param>
        /// <param name="totalNum">Total friends</param>
        /// <param name="direction">The direction of</param>
        /// <returns></returns>
        UcFriends UcFriendList(int uid, int page = 1.int pageSize = 10.int totalNum = 10, FriendDirection direction = FriendDirection.All);

        /// <summary>
        ///Point redemption request
        /// </summary>
        /// <param name="uid">Uid</param>
        /// <param name="from">The original integral</param>
        /// <param name="to">The target points</param>
        /// <param name="toAppId">Target Application ID</param>
        /// <param name="amount">The amount of points</param>
        /// <returns></returns>
        bool UcCreditExchangeRequest(int uid, int from.int to, int toAppId, int amount);

        ///<summary>
        ///Modify the picture
        ///</summary>
        ///<param name="uid">Uid</param>
        ///<param name="type"></param>
        ///<returns></returns>
        string Avatar(int uid, AvatarType type = AvatarType.Virtual);

        /// <summary>
        ///Get the avatar address
        /// </summary>
        /// <param name="uid">Uid</param>
        /// <param name="size">The size of the</param>
        /// <param name="type">type</param>
        /// <returns></returns>
        string AvatarUrl(int uid,AvatarSize size,AvatarType type = AvatarType.Virtual);

        /// <summary>
        ///Check if the avatar exists
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="size"></param>
        /// <param name="type"></param>
        /// <returns></returns>
        bool AvatarCheck(int uid, AvatarSize size = AvatarSize.Middle, AvatarType type = AvatarType.Virtual);

        /// <summary>
        ///Get label data
        /// </summary>
        /// <param name="tagName">Tag name</param>
        /// <param name="number">The number corresponding to the application ID</param>
        /// <returns></returns>
        UcTags TagGet(string tagName, IEnumerable<KeyValuePair<string.string>> number);

        /// <summary>
        ///Add event
        /// </summary>
        /// <param name="icon">Icon types include thread, POST, video, goods, reward, debate, blog, album, comment, wall, and friend</param>
        /// <param name="uid">Uid</param>
        /// <param name="userName">The user name</param>
        /// <param name="titleTemplate">The title template</param>
        /// <param name="titleData">Header data array</param>
        /// <param name="bodyTemplate">Content template</param>
        /// <param name="bodyData">Template data</param>
        /// <param name="bodyGeneral">Data used in the same event merge: specific array, only two items: name, link, reserved</param>
        /// <param name="targetIds">keep</param>
        /// <param name="images">Urls and links to related images. A picture address, a link address</param>
        /// <returns></returns>
        int FeedAdd(FeedIcon icon, int uid, string userName, string titleTemplate, string titleData, string bodyTemplate, string bodyData, string bodyGeneral, string targetIds, params string[] images);

        /// <summary>
        ///Get a Feed
        /// </summary>
        /// <param name="limit">Quantitative restrictions</param>
        /// <returns></returns>
        UcFeeds FeedGet(int limit);

        /// <summary>
        ///Get a list of apps
        /// </summary>
        /// <returns></returns>
        UcApps AppList();

        /// <summary>
        ///Add messages to the queue
        /// </summary>
        /// <param name="subject">The title</param>
        /// <param name="message">content</param>
        /// <param name="uids">Uid</param>
        /// <returns></returns>
        UcMailQueue MailQueue(string subject, string message,params int[] uids);

        /// <summary>
        ///Add messages to the queue
        /// </summary>
        /// <param name="subject">The title</param>
        /// <param name="message">content</param>
        /// <param name="emails">The target of Email</param>
        /// <returns></returns>
        UcMailQueue MailQueue(string subject, string message, params string[] emails);

        /// <summary>
        ///Add messages to the queue
        /// </summary>
        /// <param name="subject">The title</param>
        /// <param name="message">content</param>
        /// <param name="uids">Uid</param>
        /// <param name="emails">The target of email</param>
        /// <returns></returns>
        UcMailQueue MailQueue(string subject, string message, int[] uids, string[] emails);

        /// <summary>
        ///Add messages to the queue
        /// </summary>
        /// <param name="subject">The title</param>
        /// <param name="message">content</param>
        /// <param name="fromMail">Sender: This parameter is optional. The default value is empty. The email source configured on the UC background serves as the sender address</param>
        /// <param name="charset">Mail character set. This parameter is optional. The default value is GBK</param>
        /// <param name="htmlOn">This parameter is optional. The default is FALSE, that is, text mail</param>
        /// <param name="level">This parameter is optional. The default value is 1. If the value is 0, the mail will be sent immediately</param>
        /// <param name="uids">Uid</param>
        /// <param name="emails">The target of email</param>
        /// <returns></returns>
        UcMailQueue MailQueue(string subject,string message,string fromMail,string charset,bool htmlOn,int level,int[] uids,string[] emails); }}Copy the code

The interface for UCenter to call

Create a new file at your site with path /API/uc.ashx (the folder name should be capitalized) and add the following code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DS.Web.UCenter.Api;

namespace DS.Web.UCenter.Website.API
{
    /// <summary>
    /// Summary description for uc
    /// </summary>
    public class uc:UcApiBase
    {
        // The user is deleted
        public override ApiReturn DeleteUser(IEnumerable<int> ids)
        {
            throw new NotImplementedException();
        }

        // Changed the user name
        public override ApiReturn RenameUser(int uid, string oldUserName, string newUserName)
        {
            throw new NotImplementedException();
        }

        public override UcTagReturns GetTag(string tagName)
        {
            throw new NotImplementedException();
        }

        // Synchronize login
        public override ApiReturn SynLogin(int uid)
        {
            throw new NotImplementedException();
        }

        // Synchronize exit
        public override ApiReturn SynLogout()
        {
            throw new NotImplementedException();
        }

        // Update the password
        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 newNotImplementedException(); }}}Copy the code

For example, when the user logs in to discuz, UCenter will send a notification (request UC.ashx), which will pass the login user UID and other information. What we need to do is to write the login cookie or session operation in our SynLogin. Also, add processing to SynLogout to clear cookies and sessions. Synchronously log in and log out.

Pay attention to the point

  • Enable synchronous login and accept notification must be selected when adding an application
  • Fill in the required UC configuration information correctly
  • Make sure it exists on the BBS site/uc_client/data/cache/apps.phpFiles, if not from/uc_server/data/cache/apps.phpCopy a
  • To place the above configuration information on the siteWeb.configIn the

Synchronous registered

If you still need to be synchronized to register functions, refer to [[ASP.Net] UCenter login to realize multi-site synchronous exit] (http://www.jianshu.com/p/1caa425ef24b)