ดูค่า IP Address ของ client

สร้างโปรเจ็กส์แบบ Web Application แล้วดูค่า IP Address ของ client ด้วย Request.UserHostAddress

  1. ไฟล์ .aspx
  2. ไฟล์ .asmx

1.ไฟล์ .aspx

WebForm1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApp.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="ipAddress" runat="server" Text=""></asp:Label>
        </div>
    </form>
</body>
</html>

WebForm1.aspx.cs

using System;

namespace WebApp
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ipAddress.Text += string.Format("<pre>HTTP_X_FORWARDED_FOR = '{0}'</pre>", Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);
            ipAddress.Text += string.Format("<pre>REMOTE_ADDR = '{0}'</pre>", Request.ServerVariables["REMOTE_ADDR"]);
            ipAddress.Text += string.Format("<pre>UserHostAddress = '{0}'</pre>", Request.UserHostAddress);
        }
    }
}

2.ไฟล์ .asmx

using System.Web;
using System.Web.Services;

namespace WebApp
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return HttpContext.Current.Request.UserHostAddress;
        }
    }
}