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

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

在OwinSelfHost專案中,獲取客戶端的IP地址可以透過以下方法獲得:

  • base.Request.GetOwinContext().Request.RemoteIpAddress

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

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

各個類的程式碼如下:

  • Program
 1 using System;
 2 using System.Windows.Forms;
 3 
 4 namespace OwinSelfHostSample
 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 }
  • Startup
 1 using Owin;
 2 using System.Net.Http.Headers;
 3 using System.Web.Http;
 4 
 5 namespace OwinSelfHostSample
 6 {
 7     internal class Startup
 8     {
 9         public void Configuration(IAppBuilder appBuilder)
10         {
11             var config = new HttpConfiguration();
12             config.Routes.MapHttpRoute("DefaultApi", "api/{controller}/{action}/{id}", new { id = RouteParameter.Optional });
13 
14             appBuilder.UseWebApi(config);
15         }
16     }
17 }
  • MainController
 1 using System.Net.Http;
 2 using System.Web.Http;
 3 
 4 namespace OwinSelfHostSample
 5 {
 6     public class MainController : ApiController
 7     {
 8         [HttpGet, HttpPost]
 9         public IHttpActionResult GetClientIp()
10         {
11             var result = base.Request.GetOwinContext().Request.RemoteIpAddress;
12             return Ok(result);
13         }
14     }
15 }
  • MainForm
 1 using Microsoft.Owin.Hosting;
 2 using System;
 3 using System.Windows.Forms;
 4 
 5 namespace OwinSelfHostSample
 6 {
 7     internal partial class MainForm : Form
 8     {
 9         public MainForm()
10         {
11             this.InitializeComponent();
12         }
13 
14         private void button1_Click(object sender, EventArgs e)
15         {
16             WebApp.Start<Startup>("http://127.0.0.1:8090");
17             this.button1.Enabled = false;
18         }
19     }
20 }
  • 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>

相關文章