在SelfHost项目中获取客户端IP地址

在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。

项目需要添加下面的组件引用:

  • System.ServiceModel
  • System.Web

各个类的代码如下:

  • Program.cs
 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.cs
 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.cs
 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 
21             var server = new HttpSelfHostServer(config);
22             server.OpenAsync().Wait();
23 
24             this.button1.Enabled = false;
25         }
26     }
27 }
  • 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.SelfHost" version="5.3.0" targetFramework="net48" />
 8   <package id="Microsoft.AspNet.WebApi.SelfHost.zh-Hans" version="5.3.0" targetFramework="net48" />
 9   <package id="Newtonsoft.Json" version="13.0.3" targetFramework="net48" />
10   <package id="Newtonsoft.Json.Bson" version="1.0.2" targetFramework="net48" />
11   <package id="System.Buffers" version="4.5.1" targetFramework="net48" />
12   <package id="System.Memory" version="4.5.5" targetFramework="net48" />
13   <package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" />
14   <package id="System.Runtime.CompilerServices.Unsafe" version="6.0.0" targetFramework="net48" />
15   <package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net48" />
16 </packages>
posted @ 2024-07-04 22:35  【一路向东】  阅读(29)  评论(0编辑  收藏  举报