如何解决在 IIS urlrewrite 后面的 ASP.NET 应用程序上支持 HTTPS
我有一些可以通过 HTTP 和 HTTPS 访问的 ASP.NET(框架 4.8)应用程序。我想确保为此正确配置了 cookie 等。例如https 的会话 cookie 设置了 secure
标志。
- 本地 LAN 直接通过本地 HTTP 绕过,例如:http://localhost:12345/mypage?fruit=apple。 Cookie 不应设置“安全”标志。
- HTTP 到公共 IIS,它将通过 HTTP 重写到本地应用程序,例如http://example.com/myapp/mypage?fruit=apple。 Cookie 不应设置“安全”标志。
- HTTPS 到公共 IIS,它将通过 HTTP 重写到本地应用程序,例如https://example.com/myapp/mypage?fruit=apple。 Cookie 应该设置“安全”标志。因此 IIS 将拥有 TLS 证书、处理续订、SNI 等。
来自 NGINX/Rails/等,我相信 X-Forwarded-Proto
是这里的主要内容,但这似乎并没有被内置到相同的程度,例如我可能有:
location /myapp {
proxy_pass http://127.0.0.1:12345;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
}
添加到 IIS 和 ASP.NET 应用程序以确保安全等的正确配置是什么?
如果用户在任何情况下添加他们自己的标头值,是否需要担心?
到目前为止,我有以下几点:
用于 IIS 的 web.config
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="Forward Https">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^on$" />
</conditions>
<serverVariables>
<set name="HTTP_X_Forwarded_Proto" value="https" />
</serverVariables>
<action type="None" />
</rule>
<rule name="My App" stopProcessing="true">
<match url="myapp/(.*)" />
<action type="Rewrite" url="http://localhost:12345/{R:1}" logRewrittenUrl="true" />
</rule>
<allowedServerVariables>
似乎不被允许,所以在 IIS GUI 中添加了 HTTP_X_Forwarded_Proto
。
应用:
public void Configuration(IAppBuilder appBuilder)
{
// Nothing built in?
appBuilder.Use((context,next) =>
{
if (context.Request.Headers["X-Forwarded-Proto"] == "https")
{
context.Request.Scheme = "https";
}
return next();
});
// Cookie shared between different applications!
appBuilder.UseCookieAuthentication(new CookieAuthenticationOptions()
{
CookieHttpOnly = true,CookieName = ".ASPXAUTH",SlidingExpiration = true,ExpireTimeSpan = TimeSpan.FromMinutes(10),AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Active,AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,CookieSameSite = SameSiteMode.Lax,CookiePath = "/",CookieSecure = CookieSecureOption.SameAsRequest // Did not appear to use X-Forwarded-Proto
});
appBuilder.UseWebApi(config);
// ... other config stuff
}
控制器:
public class SomeController : ApiController // System.Web.Http.ApiController
{
public string Get(int id)
{
// response.Headers.AddCookies(cookie);
Request.GetOwinContext().Response.Cookies.Append("MyCookie","MyValue",new CookieOptions
{
Expires = DateTime.UtcNow + TimeSpan.FromMinutes(10),HttpOnly = true,SameSite = Microsoft.Owin.SameSiteMode.Lax,Path = "/",Secure = Request.GetOwinContext().Request.IsSecure // "SameAsRequest"
});
return "value";
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。