在IIS

时间:2015-12-30 11:25:16

标签: asp.net-core azure-web-sites asp.net-core-mvc dnx kestrel-http-server

我在获取向Azure WebApps上托管的ASP.NET 5应用程序发送请求的客户端的IP地址时遇到问题。

我的项目使用ASP.NET 5 + MVC6并在完整的CLR运行时(不是CoreCLR)上运行。我在网上搜索了答案,但没有找到任何解决方案。

使用来自Get the client's IP address的糖消费 我试过了:

HttpContext.Connection.RemoteIpAddress

但是来自HttpContext.Connection的属性都是false或null,所以这里没有运气。

我注意到以下情况:当我使用IIS Express通过Visual Studio运行我的应用程序时,来自Connection.RemoteIpAddress的IP可用。 当我通过命令行运行 dnx web 时,属性为null。似乎azure在dnx上运行我的应用程序,而不是IIS。

所以我的问题是: 如何配置项目并将其部署到Azure WebApps,因此它将在IIS后面运行,因此HttpContext.Connection将填充它的值?

我当前的配置(project.json的摘录):

(...)
    "compilationOptions": {
        "emitEntryPoint": true
      },
    "commands": {
        "web": "Microsoft.AspNet.Server.Kestrel",
        "ef": "EntityFramework.Commands"
      },

      "frameworks": {
        "dnx451": { }
      },
(...)

和Startup.cs的Configure方法

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{  
    app.UseIISPlatformHandler();
    //rest of the method...

}

1 个答案:

答案 0 :(得分:2)

Azure Web App始终使用IIS。

但是,对于ASP.NET 5代码,IIS配置为使用HttpPlatformHandler将请求重定向到Kestrel(ASP.NET 5 Web服务器)。 这是从ASP.NET Beta 8运行ASP.NET 5应用程序的方法。 您将通过打开web.config文件来查看该配置。

参考文献:

在使用Kestrel时,有一些与获取客户端IP地址相关的未解决问题,它们似乎在RC2版本中得到解决: