用户登录c#.net时如何获取系统ip地址 [英] how to get system ip address when user login in c#.net

查看:79
本文介绍了用户登录c#.net时如何获取系统ip地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想在用户登录我的网站时捕获系统IP地址

那意味着

当用户登录我的网站时,我必须捕获用户系统的IP地址

请帮助我

Hi,

I want to catch the system ip address when user login in my website

That means

when user login in my website i have to catch user system ip address

please help me

推荐答案

尝试此功能:

Try this function:

public static string getclientIP()
{
    string result= string.Empty;
    string ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
    if (!string.IsNullOrEmpty(ip))
    {
        string[] ipRange = ip.Split(',');
        int le = ipRange.Length - 1;
        result = ipRange[0];
    }
    else
    {
        result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
    }

    return result;
}



如果这解决了您的问题,请标记为答案并投票5

问候,
爱德华



Please mark as answer and vote 5 if this solved your problem

Regards,
Eduard


尝试一下:

http://stackoverflow.com/questions/735350/how-to-get-a-users-client-client-ip-address-in-asp-net [
try it:

http://stackoverflow.com/questions/735350/how-to-get-a-users-client-ip-address-in-asp-net[^]


尝试这个
protected void ip_addr()
    {
        string sIPAddress = null;
        sIPAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
        if (string.IsNullOrEmpty(sIPAddress))
            sIPAddress =Request.ServerVariables["REMOTE_ADDR"];

        SqlConnection con = new SqlConnection(conn);
        try
        {
            con.Open();
            string str = "insert into IPADDR_LOGIN(ipaddr,date_time,uname) values('" + Convert.ToString(sIPAddress) + "','" + Convert.ToString(DateTime.Now) + "','" + txt_usr.Text.ToString().Trim() + "')";
            SqlCommand cmd = new SqlCommand(str, con);
            cmd.ExecuteNonQuery();
        }
        finally
        {
            con.Close();
        }
    }


这篇关于用户登录c#.net时如何获取系统ip地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆