using System.Net; //導入命名空間 public string getLocalIP() { string strHostName = Dns.GetHostName(); //得到本機的主機名 IPHostEntry ipEntry = Dns.GetHostByName(strHostName); //取得本機IP string strAddr = ipEntry.AddressList[0].ToString(); return (strAddr); } //用法 if (rs.HasRows) { rs.Read(); context.Session["DLUserID"] = rs["U_UserID"].ToString(); context.Session["DLgetdate"] = rs["getdate"].ToString(); context.Session["DLUserDate"] = rs["U_Date"].ToString(); context.Session["DLUserIP"] = getLocalIP(); context.Response.Redirect("index_Main.aspx?Dl=" + rs["U_UserID"].ToString() + "&pas=" + rs["U_Pass"].ToString() + ""); rs.Close(); } else { context.Response.Redirect("login.aspx"); } //獲取登錄ip public string getLocalIP() { string strHostName = Dns.GetHostName(); //得到本機的主機名 IPHostEntry ipEntry = Dns.GetHostByName(strHostName); //取得本機IP string strAddr = ipEntry.AddressList[0].ToString(); return (strAddr); } public bool IsReusable { get { return false; } } } 原文:http://www.cnblogs.com/liujieblogs/p/6901279.html