如果用户通过了身份验证(通过Forms身份验证),谁知道如何在IIS中使用URL重写2来重定向到所请求URL的https版本。
基本上,我只是想确保用户通过https登录,当他们登录时,他们的整个会话都保持在https中。
我知道我可以为Forms Authentication cookie设置requireSSL属性,所以我也在这么做,但我也希望从http重定向到https。
我认为我们可以将代码放在Global.asax中来实现我想要的效果,但是如果我们可以使用URL Rewrite 2在Web.config中定义它,那么它会更整洁(并且可能对性能更好)。
发布于 2013-03-05 21:01:02
如果你使用的是ASP.NET MVC,那么这个动作过滤器会有所帮助,必须归功于原作者。
//http://weblogs.asp.net/dwahlin/archive/2009/08/25/requiring-ssl-for-asp-net-mvc-controllers.aspx
public class RequiresSsl : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var req = filterContext.HttpContext.Request;
var res = filterContext.HttpContext.Response;
//Check if we're secure or not and if we're on the local box
if (!req.IsSecureConnection && !req.IsLocal)
{
if (req.Url != null)
{
var builder = new UriBuilder(req.Url) { Scheme = Uri.UriSchemeHttps, Port = 443, Host = req.Url.Host.StartsWith("www.", StringComparison.OrdinalIgnoreCase) ? req.Url.Host : "www." + req.Url.Host };
res.Redirect(builder.Uri.ToString());
}
}
base.OnActionExecuting(filterContext);
}
}https://stackoverflow.com/questions/15224342
复制相似问题