在SelfHost專案中獲取客戶端IP地址

【一路向东】發表於2024-07-04

在SelfHost專案中,獲取客戶端的IP地址比OwinSelfHost專案要複雜一些,可以透過以下方法獲得:

  • base.Request.Properties["System.ServiceModel.Channels.RemoteEndpointMessageProperty"].Address;

建立一個SelfHost專案的大概過程:

  • 建立名稱為SelfHostSample的Windows窗體應用(.NET Framework)專案;
  • 重新命名Form1為MainForm;
  • 在NuGet包管理器中新增中新增Microsoft.AspNet.WebApi.SelfHost(注意是SelfHost,而不是OwinSelfHost);
  • 新增控制器類MainController。

各個類的程式碼如下:

  • Program
 1 using System;
 2 using System.Windows.Forms;
 3 
 4 namespace SelfHostSample
 5 {
 6     internal static class Program
 7     {
 8         [STAThread]
 9         static void Main()
10         {
11             Application.EnableVisualStyles();
12             Application.SetCompatibleTextRenderingDefault(false);
13             Application.Run(new MainForm());
14         }
15     }
16 }
  • MainController
 1 using System.ServiceModel.Channels;
 2 using System.Web;
 3 using System.Web.Http;
 4 
 5 namespace SelfHostSample
 6 {
 7     public class MainController : ApiController
 8     {
 9         [HttpGet, HttpPost]
10         public IHttpActionResult GetClientIp()
11         {
12             var result = string.Empty;
13 
14             if (base.Request.Properties.TryGetValue("MS_HttpContext", out var value1))
15             {
16                 result = ((HttpContextWrapper)value1).Request.UserHostAddress;
17             }
18 
19             if (base.Request.Properties.TryGetValue(RemoteEndpointMessageProperty.Name, out var value2))
20             {
21                 result = ((RemoteEndpointMessageProperty)value2).Address;
22             }
23 
24             return Ok(result);
25         }
26     }
27 }
  • MainForm
 1 using System;
 2 using System.Net.Http.Formatting;
 3 using System.Web.Http;
 4 using System.Web.Http.SelfHost;
 5 using System.Windows.Forms;
 6 
 7 namespace SelfHostSample
 8 {
 9     internal partial class MainForm : Form
10     {
11         public MainForm()
12         {
13             this.InitializeComponent();
14         }
15 
16         private void button1_Click(object sender, EventArgs e)
17         {
18             var config = new HttpSelfHostConfiguration("http://127.0.0.1:8090/");
19             config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{action}/{id}", new { id = RouteParameter.Optional });
20             config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(new JsonMediaTypeFormatter()));
21             
22             var server = new HttpSelfHostServer(config);
23             server.OpenAsync().Wait();
24 
25             this.button1.Enabled = false;
26         }
27     }
28 }
  • packages.config
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <packages>
 3   <package id="Microsoft.AspNet.WebApi.Client" version="6.0.0" targetFramework="net48" />
 4   <package id="Microsoft.AspNet.WebApi.Client.zh-Hans" version="6.0.0" targetFramework="net48" />
 5   <package id="Microsoft.AspNet.WebApi.Core" version="5.3.0" targetFramework="net48" />
 6   <package id="Microsoft.AspNet.WebApi.Core.zh-Hans" version="5.3.0" targetFramework="net48" />
 7   <package id="Microsoft.AspNet.WebApi.Owin" version="5.3.0" targetFramework="net48" />
 8   <package id="Microsoft.AspNet.WebApi.Owin.zh-Hans" version="5.3.0" targetFramework="net48" />
 9   <package id="Microsoft.AspNet.WebApi.OwinSelfHost" version="5.3.0" targetFramework="net48" />
10   <package id="Microsoft.Owin" version="4.2.2" targetFramework="net48" />
11   <package id="Microsoft.Owin.Host.HttpListener" version="4.2.2" targetFramework="net48" />
12   <package id="Microsoft.Owin.Hosting" version="4.2.2" targetFramework="net48" />
13   <package id="Newtonsoft.Json" version="13.0.3" targetFramework="net48" />
14   <package id="Newtonsoft.Json.Bson" version="1.0.2" targetFramework="net48" />
15   <package id="Owin" version="1.0" targetFramework="net48" />
16   <package id="System.Buffers" version="4.5.1" targetFramework="net48" />
17   <package id="System.Memory" version="4.5.5" targetFramework="net48" />
18   <package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" />
19   <package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net48" />
20   <package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net48" />
21 </packages>

相關文章