异常详细信息: System.IndexOutOfRangeException: ChatAdmin怎么解决 UserPrincipal.Login方法是怎么写的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 UserPrincipal.Login代码是什么UserPrincipal.Login(username, password,false)) 单元测试看看 UserPrincipal.Login被封装在DLL文件中,我也不知道这个方法是怎么实现的,而且第3个参数我改成TRUE,并不能解决问题明天想办法看能不能把WebGame.Module.dll,解释出来ChatAdmin这个东西跟程序有关吗? 报的错误是下标越界 我猜代码可能是 SqlDataReader["ChatAdmin"] 导致 有用户名是 ChatAdmin 的么? 还一个可能数据库中是否有ChatAdmin列。可能程序中是当做有ChatAdmin列的 代码我已经得到了/*********************************//* 这是UserPrincipal.Login的实现***//*********************************/ public static bool Login(string userName, string password, bool remember) { int num = 0; SqlDataReader userInfo = UsersDB.GetUserInfo(userName, password); if (userInfo.Read()) { if (((int) userInfo["IsLock"]) == 1) { userInfo.Close(); StringUtil.TopWindow_Location("/index.aspx", "您的帐号被锁定,无法登录。如有疑问,请与我们联系"); } UserPrincipal current = GetCurrent(); if ((current != null) && ((current.userID > 0) && (current.userID != ((int) userInfo["ID"])))) { StringUtil.TopWindow_Location("/index.aspx", "系统不允许在一个浏览器窗口用不同的用户登录!"); } int num2 = 0; if (HttpContext.Current.Request.Cookies["LoginInfo"] != null) { string str = HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies["LoginInfo"].Values["UserName"].ToString()); num2 = StringUtil.txt2int(HttpContext.Current.Request.Cookies["LoginInfo"].Values["UserID"]); if ((num2 > 0) && (num2 != ((int) userInfo["ID"]))) { new DisobeyLoginDB { UserID = (int) userInfo["ID"], UserName = userInfo["UserName"].ToString(), RelatUserID = num2, RelatUserName = str }.Add(); } } string clientIP = StringUtil.GetClientIP(); UserPrincipal principal2 = new UserPrincipal { UserID = (int) userInfo["ID"], UserName = userInfo["UserName"].ToString(), NationID = (int) userInfo["NationID"], NickName = userInfo["NickName"].ToString() }; num = (int) userInfo["AlarmCount"]; principal2.ChatAdmin = (int) userInfo["ChatAdmin"]; string sessionID = HttpContext.Current.Session.SessionID; principal2.SessionID = sessionID; principal2.Vip = (int) userInfo["Vip"]; principal2.Status = (int) userInfo["Status"]; principal2.UnionID = (int) userInfo["UnionID"]; principal2.IP = clientIP; Hashtable hashtable = (Hashtable) HttpContext.Current.Application["Online"]; string key = principal2.UserID.ToString(); int num3 = 0; if (hashtable != null) { if (!(!hashtable.ContainsValue(HttpContext.Current.Session.SessionID) || hashtable.Contains(key))) { num3 = 1; StringUtil.TopWindow_Location("/index.aspx", "系统不允许在一个浏览器窗口用不同的用户登录!"); } if (hashtable.Contains(key)) { hashtable[key] = principal2.SessionID; } else { hashtable.Add(key, principal2.SessionID); } } else { hashtable = new Hashtable(); hashtable.Add(key, principal2.SessionID); } HttpContext.Current.Application.Lock(); HttpContext.Current.Application["Online"] = hashtable; HttpContext.Current.Application.UnLock(); FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, principal2.UserName, DateTime.Now, DateTime.Now.AddMinutes(60.0), false, principal2.ToString()); string str5 = FormsAuthentication.Encrypt(ticket); HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, str5); if (remember) { cookie.Expires = DateTime.Now.AddDays(60.0); } HttpContext.Current.Response.Cookies.Add(cookie); userInfo.Close(); HttpCookie cookie2 = new HttpCookie("LoginInfo"); cookie2.Values.Add("UserID", principal2.userID.ToString()); cookie2.Values.Add("UserName", HttpUtility.UrlEncode(principal2.UserName.ToString())); cookie2.Expires = DateTime.Now.AddHours(6.0); HttpContext.Current.Response.AppendCookie(cookie2); UsersDB.LoginProcess(principal2.UserID, clientIP, principal2.SessionID, num3); if (num3 == 1) { new MessageDB { Subject = "系统警告", Type = 3, Content = "系统发现您在同一台电脑上,用不同的帐号登陆。这是游戏规则不允许的,造成的后果自负!", ToUserID = principal2.userID }.Add(); } return true; } userInfo.Close(); return false; } /**************************************//* 这是UserPrincipal.ChatAdmin的实现***//**************************************/ public int ChatAdmin { get { return this.chatAdmin; } set { this.chatAdmin = value; } } 重新发一个,上面那个不好看的/**********************************//* 这是UserPrincipal.Login的实现***//**********************************/ public static bool Login(string userName, string password, bool remember) { int num = 0; SqlDataReader userInfo = UsersDB.GetUserInfo(userName, password); if (userInfo.Read()) { if (((int) userInfo["IsLock"]) == 1) { userInfo.Close(); StringUtil.TopWindow_Location("/index.aspx", "您的帐号被锁定,无法登录。如有疑问,请与我们联系"); } UserPrincipal current = GetCurrent(); if ((current != null) && ((current.userID > 0) && (current.userID != ((int) userInfo["ID"])))) { StringUtil.TopWindow_Location("/index.aspx", "系统不允许在一个浏览器窗口用不同的用户登录!"); } int num2 = 0; if (HttpContext.Current.Request.Cookies["LoginInfo"] != null) { string str = HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies["LoginInfo"].Values["UserName"].ToString()); num2 = StringUtil.txt2int(HttpContext.Current.Request.Cookies["LoginInfo"].Values["UserID"]); if ((num2 > 0) && (num2 != ((int) userInfo["ID"]))) { new DisobeyLoginDB { UserID = (int) userInfo["ID"], UserName = userInfo["UserName"].ToString(), RelatUserID = num2, RelatUserName = str }.Add(); } } string clientIP = StringUtil.GetClientIP(); UserPrincipal principal2 = new UserPrincipal { UserID = (int) userInfo["ID"], UserName = userInfo["UserName"].ToString(), NationID = (int) userInfo["NationID"], NickName = userInfo["NickName"].ToString() }; num = (int) userInfo["AlarmCount"]; principal2.ChatAdmin = (int) userInfo["ChatAdmin"]; string sessionID = HttpContext.Current.Session.SessionID; principal2.SessionID = sessionID; principal2.Vip = (int) userInfo["Vip"]; principal2.Status = (int) userInfo["Status"]; principal2.UnionID = (int) userInfo["UnionID"]; principal2.IP = clientIP; Hashtable hashtable = (Hashtable) HttpContext.Current.Application["Online"]; string key = principal2.UserID.ToString(); int num3 = 0; if (hashtable != null) { if (!(!hashtable.ContainsValue(HttpContext.Current.Session.SessionID) || hashtable.Contains(key))) { num3 = 1; StringUtil.TopWindow_Location("/index.aspx", "系统不允许在一个浏览器窗口用不同的用户登录!"); } if (hashtable.Contains(key)) { hashtable[key] = principal2.SessionID; } else { hashtable.Add(key, principal2.SessionID); } } else { hashtable = new Hashtable(); hashtable.Add(key, principal2.SessionID); } HttpContext.Current.Application.Lock(); HttpContext.Current.Application["Online"] = hashtable; HttpContext.Current.Application.UnLock(); FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, principal2.UserName, DateTime.Now, DateTime.Now.AddMinutes(60.0), false, principal2.ToString()); string str5 = FormsAuthentication.Encrypt(ticket); HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, str5); if (remember) { cookie.Expires = DateTime.Now.AddDays(60.0); } HttpContext.Current.Response.Cookies.Add(cookie); userInfo.Close(); HttpCookie cookie2 = new HttpCookie("LoginInfo"); cookie2.Values.Add("UserID", principal2.userID.ToString()); cookie2.Values.Add("UserName", HttpUtility.UrlEncode(principal2.UserName.ToString())); cookie2.Expires = DateTime.Now.AddHours(6.0); HttpContext.Current.Response.AppendCookie(cookie2); UsersDB.LoginProcess(principal2.UserID, clientIP, principal2.SessionID, num3); if (num3 == 1) { new MessageDB { Subject = "系统警告", Type = 3, Content = "系统发现您在同一台电脑上,用不同的帐号登陆。这是游戏规则不允许的,造成的后果自负!", ToUserID = principal2.userID }.Add(); } return true; } userInfo.Close(); return false; } 查询的SQL 返回的结果集 没有 ChatAdmin 列 数据库中没有ChatAdmin,我在UserPrincipal这个类中看到它的 public class UserPrincipal { private int chatAdmin = 0; private string ip = ""; private int nationID = 0; private string nickName = ""; private string sessionID = ""; private int status = 1; private int unionID = 0; private int userID = 0; private string userName = ""; private int vip = 2; public int ChatAdmin { get { return this.chatAdmin; } set { this.chatAdmin = value; } }这不是函数,但我以前没见过这种写法,声明类的时候还赋了初值 0 的,不懂有什么用?? 这是属性。你要不改DLL 要不改表结构 好像都不太容易,发布这个代码的那个帖子,被转了很多次,有人说可以用,而且写的是商业版本,所以我想应该是在某一个比较不起眼的地方出现了异常。报异常是System.IndexOutOfRangeException: ChatAdmin而报异常的地方却是if (UserPrincipal.Login(username, password,false))这里,他们之间有什么关系,还没找到 抱歉原来对sandy945的误解,的确是这个问题 这种效果如何做??求教了~~~~~ ASP.NET MVC 数据显示? NET 截取字符串 报了这个错 求一个 .NET 2005 下面的分页控件源码 高价急求软件开发高手! 关于日期比较 简单问题,高手请进 (简单是对于高手) 关于DropDownList的一个问题 来者有分:如何获得点击DataGrid控件单元格的内容? ASP.NET网站二次开发问题 求一正则表达式
/*********************************/
/* 这是UserPrincipal.Login的实现***/
/*********************************/
public static bool Login(string userName, string password, bool remember)
{
int num = 0;
SqlDataReader userInfo = UsersDB.GetUserInfo(userName, password);
if (userInfo.Read())
{
if (((int) userInfo["IsLock"]) == 1)
{
userInfo.Close();
StringUtil.TopWindow_Location("/index.aspx", "您的帐号被锁定,无法登录。如有疑问,请与我们联系");
}
UserPrincipal current = GetCurrent();
if ((current != null) && ((current.userID > 0) && (current.userID != ((int) userInfo["ID"]))))
{
StringUtil.TopWindow_Location("/index.aspx", "系统不允许在一个浏览器窗口用不同的用户登录!");
}
int num2 = 0;
if (HttpContext.Current.Request.Cookies["LoginInfo"] != null)
{
string str = HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies["LoginInfo"].Values["UserName"].ToString());
num2 = StringUtil.txt2int(HttpContext.Current.Request.Cookies["LoginInfo"].Values["UserID"]);
if ((num2 > 0) && (num2 != ((int) userInfo["ID"])))
{
new DisobeyLoginDB { UserID = (int) userInfo["ID"], UserName = userInfo["UserName"].ToString(), RelatUserID = num2, RelatUserName = str }.Add();
}
}
string clientIP = StringUtil.GetClientIP();
UserPrincipal principal2 = new UserPrincipal {
UserID = (int) userInfo["ID"],
UserName = userInfo["UserName"].ToString(),
NationID = (int) userInfo["NationID"],
NickName = userInfo["NickName"].ToString()
};
num = (int) userInfo["AlarmCount"];
principal2.ChatAdmin = (int) userInfo["ChatAdmin"];
string sessionID = HttpContext.Current.Session.SessionID;
principal2.SessionID = sessionID;
principal2.Vip = (int) userInfo["Vip"];
principal2.Status = (int) userInfo["Status"];
principal2.UnionID = (int) userInfo["UnionID"];
principal2.IP = clientIP;
Hashtable hashtable = (Hashtable) HttpContext.Current.Application["Online"];
string key = principal2.UserID.ToString();
int num3 = 0;
if (hashtable != null)
{
if (!(!hashtable.ContainsValue(HttpContext.Current.Session.SessionID) || hashtable.Contains(key)))
{
num3 = 1;
StringUtil.TopWindow_Location("/index.aspx", "系统不允许在一个浏览器窗口用不同的用户登录!");
}
if (hashtable.Contains(key))
{
hashtable[key] = principal2.SessionID;
}
else
{
hashtable.Add(key, principal2.SessionID);
}
}
else
{
hashtable = new Hashtable();
hashtable.Add(key, principal2.SessionID);
}
HttpContext.Current.Application.Lock();
HttpContext.Current.Application["Online"] = hashtable;
HttpContext.Current.Application.UnLock();
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, principal2.UserName, DateTime.Now, DateTime.Now.AddMinutes(60.0), false, principal2.ToString());
string str5 = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, str5);
if (remember)
{
cookie.Expires = DateTime.Now.AddDays(60.0);
}
HttpContext.Current.Response.Cookies.Add(cookie);
userInfo.Close();
HttpCookie cookie2 = new HttpCookie("LoginInfo");
cookie2.Values.Add("UserID", principal2.userID.ToString());
cookie2.Values.Add("UserName", HttpUtility.UrlEncode(principal2.UserName.ToString()));
cookie2.Expires = DateTime.Now.AddHours(6.0);
HttpContext.Current.Response.AppendCookie(cookie2);
UsersDB.LoginProcess(principal2.UserID, clientIP, principal2.SessionID, num3);
if (num3 == 1)
{
new MessageDB { Subject = "系统警告", Type = 3, Content = "系统发现您在同一台电脑上,用不同的帐号登陆。这是游戏规则不允许的,造成的后果自负!", ToUserID = principal2.userID }.Add();
}
return true;
}
userInfo.Close();
return false;
}
/* 这是UserPrincipal.ChatAdmin的实现***/
/**************************************/
public int ChatAdmin
{
get
{
return this.chatAdmin;
}
set
{
this.chatAdmin = value;
}
}
/* 这是UserPrincipal.Login的实现***/
/**********************************/ public static bool Login(string userName, string password, bool remember)
{
int num = 0;
SqlDataReader userInfo = UsersDB.GetUserInfo(userName, password);
if (userInfo.Read())
{
if (((int) userInfo["IsLock"]) == 1)
{
userInfo.Close();
StringUtil.TopWindow_Location("/index.aspx", "您的帐号被锁定,无法登录。如有疑问,请与我们联系");
}
UserPrincipal current = GetCurrent();
if ((current != null) && ((current.userID > 0) && (current.userID != ((int) userInfo["ID"]))))
{
StringUtil.TopWindow_Location("/index.aspx", "系统不允许在一个浏览器窗口用不同的用户登录!");
}
int num2 = 0;
if (HttpContext.Current.Request.Cookies["LoginInfo"] != null)
{
string str = HttpUtility.UrlDecode(HttpContext.Current.Request.Cookies["LoginInfo"].Values["UserName"].ToString());
num2 = StringUtil.txt2int(HttpContext.Current.Request.Cookies["LoginInfo"].Values["UserID"]);
if ((num2 > 0) && (num2 != ((int) userInfo["ID"])))
{
new DisobeyLoginDB { UserID = (int) userInfo["ID"], UserName = userInfo["UserName"].ToString(), RelatUserID = num2, RelatUserName = str }.Add();
}
}
string clientIP = StringUtil.GetClientIP();
UserPrincipal principal2 = new UserPrincipal {
UserID = (int) userInfo["ID"],
UserName = userInfo["UserName"].ToString(),
NationID = (int) userInfo["NationID"],
NickName = userInfo["NickName"].ToString()
};
num = (int) userInfo["AlarmCount"];
principal2.ChatAdmin = (int) userInfo["ChatAdmin"];
string sessionID = HttpContext.Current.Session.SessionID;
principal2.SessionID = sessionID;
principal2.Vip = (int) userInfo["Vip"];
principal2.Status = (int) userInfo["Status"];
principal2.UnionID = (int) userInfo["UnionID"];
principal2.IP = clientIP;
Hashtable hashtable = (Hashtable) HttpContext.Current.Application["Online"];
string key = principal2.UserID.ToString();
int num3 = 0;
if (hashtable != null)
{
if (!(!hashtable.ContainsValue(HttpContext.Current.Session.SessionID) || hashtable.Contains(key)))
{
num3 = 1;
StringUtil.TopWindow_Location("/index.aspx", "系统不允许在一个浏览器窗口用不同的用户登录!");
}
if (hashtable.Contains(key))
{
hashtable[key] = principal2.SessionID;
}
else
{
hashtable.Add(key, principal2.SessionID);
}
}
else
{
hashtable = new Hashtable();
hashtable.Add(key, principal2.SessionID);
}
HttpContext.Current.Application.Lock();
HttpContext.Current.Application["Online"] = hashtable;
HttpContext.Current.Application.UnLock();
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, principal2.UserName, DateTime.Now, DateTime.Now.AddMinutes(60.0), false, principal2.ToString());
string str5 = FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, str5);
if (remember)
{
cookie.Expires = DateTime.Now.AddDays(60.0);
}
HttpContext.Current.Response.Cookies.Add(cookie);
userInfo.Close();
HttpCookie cookie2 = new HttpCookie("LoginInfo");
cookie2.Values.Add("UserID", principal2.userID.ToString());
cookie2.Values.Add("UserName", HttpUtility.UrlEncode(principal2.UserName.ToString()));
cookie2.Expires = DateTime.Now.AddHours(6.0);
HttpContext.Current.Response.AppendCookie(cookie2);
UsersDB.LoginProcess(principal2.UserID, clientIP, principal2.SessionID, num3);
if (num3 == 1)
{
new MessageDB { Subject = "系统警告", Type = 3, Content = "系统发现您在同一台电脑上,用不同的帐号登陆。这是游戏规则不允许的,造成的后果自负!", ToUserID = principal2.userID }.Add();
}
return true;
}
userInfo.Close();
return false;
}
{
private int chatAdmin = 0;
private string ip = "";
private int nationID = 0;
private string nickName = "";
private string sessionID = "";
private int status = 1;
private int unionID = 0;
private int userID = 0;
private string userName = "";
private int vip = 2;
public int ChatAdmin
{
get
{
return this.chatAdmin;
}
set
{
this.chatAdmin = value;
}
}这不是函数,但我以前没见过这种写法,声明类的时候还赋了初值 0 的,不懂有什么用??
而报异常的地方却是if (UserPrincipal.Login(username, password,false))这里,他们之间有什么关系,还没找到
抱歉原来对sandy945的误解,的确是这个问题