gpt4 book ai didi

asp.net-mvc - 如何在 asp.net mvc 中从 https 模式跳到 http 模式

转载 作者:行者123 更新时间:2023-12-05 00:08:20 25 4
gpt4 key购买 nike

我通过在 Controller 操作上添加属性 [RequireSSL] 使我的登录页面启用了 Https,它工作正常。但是成功登录后它仍然在https环境中,但是该页面是非https页面。
谁能给我解决方法如何从 https 跳到 http 模式?
在这方面的任何帮助将不胜感激。

最佳答案

您基本上需要做相反的事情,即具有 [DoesNotRequireSSL] 属性,它实际上与 {RequireSSL] 属性相反,即重定向到 http 协议(protocol)

public class DoesNotRequireSSL: ActionFilterAttribute 
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var request = filterContext.HttpContext.Request;
var response = filterContext.HttpContext.Response;

if (request.IsSecureConnection && !request.IsLocal)
{
string redirectUrl = request.Url.ToString().Replace("https:", "http:");
response.Redirect(redirectUrl);
}
base.OnActionExecuting(filterContext);
}
}

此外,如果您想确保多个页面具有此行为,您可以设置一个基本 Controller ,所有非 http Controller 都可以从该 Controller 继承,因此您不必担心必须为每个需要重复的页面重复自己这。

关于asp.net-mvc - 如何在 asp.net mvc 中从 https 模式跳到 http 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1566928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com