您的位置:首页 > 编程语言 > C#

C#的百度地图开发(六)用户访问网页的IP记录

2015-01-14 11:30 260 查看
在《C#的百度地图开发(五)IP定位》一文中,我们可以通过IP来定位,那这些IP要如何去获取呢?对于网站,我们可以能过用户访问页面时,记录其IP。因为用户访问页面时,是向服务器发起了请求,我们只要在服务器端作相应的处理,就可以记录了。如果使用Asp.Net开发站点,可以借助Global.asax(全局应用程序类)来记录。

public class Global : System.Web.HttpApplication
{

protected void Application_Start(object sender, EventArgs e)
{

}

protected void Session_Start(object sender, EventArgs e)
{

}

protected void Application_BeginRequest(object sender, EventArgs e)
{

}

protected void Application_AuthenticateRequest(object sender, EventArgs e)
{

}

protected void Application_Error(object sender, EventArgs e)
{

}

protected void Session_End(object sender, EventArgs e)
{

}

protected void Application_End(object sender, EventArgs e)
{

}

protected void Application_PostAuthenticateRequest(object sender, EventArgs e)
{
// 提取窗体身份验证 cookie
string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = HttpContext.Current.Request.Cookies[cookieName];
if (null == authCookie)
{
// 没有身份验证 cookie。
return;
}
FormsAuthenticationTicket authTicket = null;
authTicket = FormsAuthentication.Decrypt(authCookie.Value);

if (null == authTicket)
{
// 无法解密 Cookie。
return;
}

string[] roles = authTicket.UserData.Split(new char[] { ',' });
FormsIdentity id = new FormsIdentity(authTicket);
System.Security.Principal.GenericPrincipal principal = new System.Security.Principal.GenericPrincipal(id, roles);
HttpContext.Current.User = principal;

String visitIP = Request.UserHostAddress;//访问者的IP
String visitUrl = Request.FilePath;//访问的页面(页面的JS、CSS也会获取,在记录时需要加以区分)
}
}
注:

(1).Application_PostAuthenticateRequest是页面访问发起验证请求时都会触发该函数,所以可以在此记录。

(2).记录IP信息,用户信息到数据库中,还可以进一步分析出某一时间段的在线用户。

得到了IP后,就可以按前面的来进行百度地图的定位了。

转载请注明出处http://blog.csdn.net/xxdddail/article/details/42707243。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: