【发布时间】:2015-12-30 11:25:16
【问题描述】:
我在获取向 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 时,属性为空。似乎 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...
}
【问题讨论】:
-
我已经回答了问题的第一部分(IIS/Kestrel)。关于客户端IP,包Microsoft.AspNet.IISPlatformHandler和中间件UseIISPlatformHandler()应该按照github.com/aspnet/Templates/issues/189转发客户端IP。你确定要用吗?
-
这会重定向我本地实例上的 IP,但不会重定向到已部署的 Azure 网站。
标签: asp.net-core azure-web-app-service asp.net-core-mvc dnx kestrel-http-server