在做Discuz!NT 2.0与自己的系统整合过程中,使用的是Discuz!NT 2.0的用户数据作为我的用户数据。
把Discuz!NT 2.0文件存放入bbs目录,把bbs目录设置为虚拟目录。把DNT.config拷贝到根目录。
登录很简单直接设置表单吧
<form action="bbs/login.aspx?reurl=<%= reurl %>" method="post">
<div id="Main" class="sign">
<h2>用户登录</h2>
<div class="meat">
<form>
<div>
<label class="label_input" for="mail">用户名:</label>
<input type="text" id="mail" class="txt" name="username" />
</div>
<div>
<label class="label_input" for="password">密码:</label>
<input type="password" id="password" class="txt" name="password" />
</div>
<div class="act">
<input type="submit" value="登录" class="btn" />
<a href="bbs/getpassword.aspx" target="_blank">忘记密码?</a>
</div>
</form>
</div>
</div>
</form>
reurl 的值
protected string reurl = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
if (Request.UrlReferrer != null)
reurl = HttpUtility.UrlEncode(Request.UrlReferrer.PathAndQuery);
else
reurl = HttpUtility.UrlEncode("/default.aspx");
}
通过以下代码拿到了用户ID
{
int uid = 0;
if (Request.Cookies["dnt"] != null && Request.Cookies["dnt"].Values["userid"] != null)
int.TryParse(Request.Cookies.Get("dnt").Values.Get("userid"), out uid);
读取一个XML文件获取人物信息。因为我在配置过程中总是有配置文件目录存放错误问题,因此,直接用了个XML文件,不用去仔细看目录怎么配置了。
XML文件代码是这样的
<%@ Import namespace="System.Data" %>
<%@ Import namespace="Discuz.Common" %>
<%@ Import namespace="Discuz.Forum" %>
<%@ Import namespace="Discuz.Entity" %>
<script runat="server">
override protected void OnInit(EventArgs e)
{
base.OnInit(e);
int userid = 0;
int.TryParse(Request.QueryString["u"], out userid);
StringBuilder sb = new StringBuilder();
if (userid > 0)
{
UserInfo ui = Users.GetUserInfo(userid);
sb.Append("<?xml version=\"1.0\"?>\n");
sb.Append("<root>\n");
sb.Append("\t<username>");
sb.Append(ui.Username);
sb.Append("\t</username>\n");
sb.Append("\t<userid>");
sb.Append(ui.Uid);
sb.Append("\t</userid>\n");
sb.Append("\t<userkey>");
sb.Append(ui.Password);
sb.Append("\t</userkey>\n");
sb.Append("</root>\n");
}
else
{
sb.Append("<?xml version=\"1.0\"?>\n");
sb.Append("<root>\n");
sb.Append("\t<username>");
sb.Append("游客");
sb.Append("\t</username>\n");
sb.Append("\t<userid>");
sb.Append(-1);
sb.Append("\t</userid>\n");
sb.Append("\t<userkey>");
sb.Append(7);
sb.Append("\t</userkey>\n");
sb.Append("</root>\n");
}
Response.Write(sb.ToString());
}
</script>
做退出的时候要取userkey,我看了他的源代码
aspx/1/usercp.aspx的退出链接是这样的,在101行
templateBuilder.Append(" <a href=\"" + forumurl.ToString() + "logout.aspx?userkey=" + userkey.ToString() + "\" class=\"reg\">退出</a>\r\n");
从第一行<%@ Page language="c#" Codebehind="usercp.aspx.cs" AutoEventWireup="false" EnableViewState="false" Inherits="Discuz.ForumPage.usercp" %>看到该页使用了Dll文件里的usercp类。经查看是Discuz.Web.Dll里的usercp类,其父类是Discuz.Web.UI.Dll里的BasePage,而这个userkey正是在BasePage类定义的字段。
在BasePage的构造函数中给userkey赋值了
this.userid = this.oluserinfo.Userid;
this.usergroupid = this.oluserinfo.Groupid;
this.username = this.oluserinfo.Username;
this.password = this.oluserinfo.Password;
if (this.password.Length > 0x10)
{
this.userkey = this.password.Substring(4, 8).Trim();
}
else
{
this.userkey = "";
}
根据这样我也就可以给我使用的userkey赋值为
while (reader.Read())
{
if (reader.Name.Equals("username"))
username = reader.ReadInnerXml();
if (reader.Name.Equals("userid"))
userid = ConvertHelper.str_to_int(reader.ReadInnerXml());
if (reader.Name.Equals("userkey"))
{
string password = reader.ReadInnerXml();
if(password.Length > 0x10)
userkey = password.Substring(4, 8).Trim(); ;
}
}
Dll里实际退出代码是这样的
{
base.pagetitle = "用户退出";
base.username = "游客";
int userid = base.userid;
base.userid = -1;
StringBuilder builder = new StringBuilder();
builder.Append("if (top.document.getElementById('leftmenu')){");
builder.Append("\t\ttop.frames['leftmenu'].location.reload();");
builder.Append("}");
base.AddScript(builder.ToString());
base.SetUrl(Utils.UrlDecode(ForumUtils.GetReUrl()));
base.SetMetaRefresh();
base.SetShowBackLink(false);
if (DNTRequest.GetString("userkey") == base.userkey)
{
base.AddMsgLine("已经清除了您的登录信息, 稍后您将以游客身份返回首页");
Users.UpdateOnlineTime(userid);
OnlineUsers.DeleteRows(base.olid);
ForumUtils.ClearUserCookie();
Utils.WriteCookie(Utils.GetTemplateCookieName(), "", -999999);
}
else
{
base.AddMsgLine("无法确定您的身份, 稍后返回首页");
}
}