首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当用户使用URL重写2登录时重定向至HTTPS

当用户使用URL重写2登录时重定向至HTTPS
EN

Stack Overflow用户
提问于 2013-03-05 20:54:34
回答 1查看 335关注 0票数 1

如果用户通过了身份验证(通过Forms身份验证),谁知道如何在IIS中使用URL重写2来重定向到所请求URL的https版本。

基本上,我只是想确保用户通过https登录,当他们登录时,他们的整个会话都保持在https中。

我知道我可以为Forms Authentication cookie设置requireSSL属性,所以我也在这么做,但我也希望从http重定向到https。

我认为我们可以将代码放在Global.asax中来实现我想要的效果,但是如果我们可以使用URL Rewrite 2在Web.config中定义它,那么它会更整洁(并且可能对性能更好)。

EN

回答 1

Stack Overflow用户

发布于 2013-03-05 21:01:02

如果你使用的是ASP.NET MVC,那么这个动作过滤器会有所帮助,必须归功于原作者。

代码语言:javascript
复制
//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);
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15224342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档