程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> DiscuzNT 論壇與主站的同步登錄與退出

DiscuzNT 論壇與主站的同步登錄與退出

編輯:ASP.NET基礎
論壇域名是:forum.web.net(虛擬)
網站域名是:www.web.net(虛擬too)
先用管理員帳戶進入論壇後台在基本設置中,將“身份驗證Cookie域:”設置為.web.net
論壇配置好後,復制bin文件夾的 Discuz.Forum.dll,Discuz.Entity.dll,Discuz.Data.dll,Discuz.Data.SqlServer.dll 和Discuz.Common.dll到網站的bin下。同樣,DNT.Config也放到網站根目錄
貼個代碼看,更清楚些。
復制代碼 代碼如下:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using Discuz.Forum;
using Discuz.Common;
using Discuz.Entity;

namespace WebSite
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//下面的判斷作用是:檢測cookie中是不是有論壇的userid,如果有就說明有用戶登錄
if (Request.Cookies["dnt"] != null && Request.Cookies["dnt"].Values["userid"] != null)
{
//從cookie中獲得UserID
int uid = Convert.ToInt32(Request.Cookies["dnt"].Values["userid"].ToString());
//得到這個用戶的全部信息
UserInfo a = Discuz.Forum.Users.GetUserInfo(uid);
//打印出來看看對不對。
Response.Write(a.Username);
//這句代碼是退出!清除Cookie!!!
ForumUtils.ClearUserCookie();
}
else //沒有用戶?看我怎麼登錄的!
{
//先設倆變量
string uname = "rohan";
string upass = "mypassword";
//驗證用戶登錄 如果正確返回UserID,否則返回-1
int uid = Users.CheckPassword(uname, upass, true);
if (uid!=-1)
{
//下面都是官方整合說明文檔裡的內容了
LoginLogs.DeleteLoginLog(DNTRequest.GetIP());
//根據積分公式刷新用戶總積分
UserCredits.UpdateUserCredits(uid);
//寫入用戶登錄後的cookie
//××××××注意××××××這裡有點特殊,原文是
//ForumUtils.WriteUserCookie(uid, Utils.StrToInt(DNTRequest.GetString("expires"), -1), config.Passwordkey, DNTRequest.GetInt("templateid", 0), DNTRequest.GetInt("loginmode", -1));
//這裡我把config.Passwordkey直接從論壇config文件夾下的general.config文件的節點提取出來了。這樣就不用把論壇的相關文件拷貝到網站上了
ForumUtils.WriteUserCookie(uid, Utils.StrToInt(DNTRequest.GetString("expires"), -1), "R254842J4Z", DNTRequest.GetInt("templateid", 0), DNTRequest.GetInt("loginmode", -1));

//更新該用戶最後訪問時間
Users.UpdateUserLastvisit(uid, DNTRequest.GetIP());

}
}

}

}
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved