本文介绍了如何使用global.asax将http转换为https和其他方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 HTTPS重定向无法使用Global.asax。 我的代码如下: <%@ 应用程序 语言 = C# %> < script runat = 服务器 > // Alex于2014年1月29日添加,以清除http / https问题而不启用SSL证书 private void RedirectToCor rectSSLScheme() { Uri pageRequest = Request.Url; string requestPath = pageRequest.GetLeftPart(UriPartial.Path).ToLower(); requestPath = Server.UrlDecode(requestPath); // 如果给定页面应该是安全的,则PageIsSecure返回。我 // 维护安全页面列表或 // XML配置中的安全目录。 // bool securePage = GetSecurePages()。PageIsSecure(requestPath); // 我们可以设置卡塔尔服务器的IP地址而不是localhost if (pageRequest.Scheme == https&& pageRequest.Host.Contains( localhost)&& requestPath。包含( .aspx)) { Response.Redirect( http:// + pageRequest.Host + pageRequest.PathAndQuery,真); // Response.Redirect(requestPath,true); } // 由Alex添加以检查卡塔尔服务器的外部网址 // 我们可以设置卡塔尔服务器的IP地址 而不是localhost > if (pageRequest.Scheme == http&& pageRequest。 Host.ToString()!= localhost&& requestPath.Contains( .aspx)) { Response.Redirect( https:// + pageRequest.Host + pageRequest.PathAndQuery,真); // Response.Redirect(requestPath,true); } } void Application_BeginRequest( object sender,EventArgs e) { // Alex于2014年1月29日添加的功能清除卡塔尔服务器下的http / https问题 RedirectToCorrectSSLScheme(); } void Application_Start( object sender ,EventArgs e) { // 在应用程序启动时运行的代码 } void Application_End( object sender,EventArgs e) { // 运行于的代码应用程序关闭 } void Application_Error(对象发​​件人,EventArgs e) { // 运行时的代码发生未处理的错误 } void Session_Start( object sender,EventArgs e) { // 代码在新会话开始时运行 } void Session_End( object sender,EventArgs e) { // 会话结束时运行的代码。 // 注意:仅当sessionstate模式时才会引发Session_End事件 // 在Web.config文件中设置为InProc。如果会话模式设置为StateServer // 或SQLServer,则不会引发该事件。 } < / script > 即使进入网址,网址也不会从http更改为https if循环正常。它仍然保持为http:// localhost / ..... 解决方案 试试这个 http://www.youtube.com/watch?v=GADxwe4XY5Y&list=PL6n9fhu94yhXQS_p1i-HLIftB9Y7Vnxlo [ ^ ] HTTPS redirection not working using Global.asax. My code is as follows:<%@ Application Language="C#" %><script runat="server"> //Added by Alex on Jan 29,2014 to clear http/https issue without enabling SSL Certificate private void RedirectToCorrectSSLScheme() { Uri pageRequest = Request.Url; string requestPath = pageRequest.GetLeftPart(UriPartial.Path).ToLower(); requestPath = Server.UrlDecode(requestPath); // PageIsSecure returns if the given page should be secure or not. I //maintain a list of secure pages or //secure directory in an XML config. // bool securePage = GetSecurePages().PageIsSecure(requestPath); //Instead of localhost we can set the IP Address of Qatar server if (pageRequest.Scheme == "https" && pageRequest.Host.Contains("localhost") && requestPath.Contains(".aspx")) { Response.Redirect("http://" + pageRequest.Host + pageRequest.PathAndQuery, true); // Response.Redirect(requestPath, true); } //Added by Alex to check external url of qatar server //Instead of localhost we can set the IP Address of Qatar server if (pageRequest.Scheme == "http" && pageRequest.Host.ToString()!="localhost" && requestPath.Contains(".aspx")) { Response.Redirect("https://" + pageRequest.Host + pageRequest.PathAndQuery, true); //Response.Redirect(requestPath, true); } } void Application_BeginRequest(object sender, EventArgs e) { //Function Added by Alex on Jan 29,2014 to clear http/https issue under Qatar Server RedirectToCorrectSSLScheme(); } void Application_Start(object sender, EventArgs e) { // Code that runs on application startup } void Application_End(object sender, EventArgs e) { // Code that runs on application shutdown } void Application_Error(object sender, EventArgs e) { // Code that runs when an unhandled error occurs } void Session_Start(object sender, EventArgs e) { // Code that runs when a new session is started } void Session_End(object sender, EventArgs e) { // Code that runs when a session ends. // Note: The Session_End event is raised only when the sessionstate mode // is set to InProc in the Web.config file. If session mode is set to StateServer // or SQLServer, the event is not raised. } </script>It isn't changing from http to https in the url even though its entering the if loop properly. Its still remaining as http://localhost/..... 解决方案 Try thishttp://www.youtube.com/watch?v=GADxwe4XY5Y&list=PL6n9fhu94yhXQS_p1i-HLIftB9Y7Vnxlo[^] 这篇关于如何使用global.asax将http转换为https和其他方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-14 20:39