如何在浏览器中将URL从http更改为https [英] how to change the url from http to https in browser

查看:922
本文介绍了如何在浏览器中将URL从http更改为https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请一些人告诉我如何在浏览器中更改URL:http://Login.aspx tp https://Login.aspx

我想保护我的页面免受此功能的影响,请一些一个告诉你如何在asp.net c#

解决方案

对这个论坛的一点研究导致:

自动从HTTP切换到HTTPS [ ^ ]


以下是更多链接:



http://www.xdevsoftware.com/blog /post/Redirect-from-Http-to-Https-in-ASPNET.aspx [ ^ ]

http://weblogs.asp.net/scottgu/archive/2007/04/06/tip-trick-enabling-ssl-on -iis7-using-self-signed-certificates.aspx [ ^ ]


在Global.asax文件中< br $> b $ b

 受保护  void  Application_BeginRequest( object  sender,EventArgs e)
{
string 路径= HttpContext.Current.Request.Url.AbsolutePath;

if (HttpContext.Current.Request.ServerVariables [ HTTPS] == on
{
if (SecurePath.IsSecure(path))
{
/ / 什么都不做
}
else
{
HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri.Replace( https :// http://));
return ;
}
}

if (HttpContext.Current.Request.ServerVariables [ HTTPS]!= on
{
if (SecurePath.IsSecure(path))
{
< span class =code-comment> // 重定向到https版本
HttpContext.Current.Response.Redirect(HttpContext.Current.Request) .Url.AbsoluteUri.Replace( http:// https://));
}
}


}





这是定义for SecurePath Class



   #regionSecurePath Class
public class SecurePath
{

< span class =code-keyword> public static bool IsSecure( string 路径)
{
List< securepage> lstPages = new 列表< securepage>();

bool isSecure = false ;

尝试
{
// 填写web.config中定义的页面列表
NameValueCollection sectionPages =(NameValueCollection)ConfigurationManager.GetSection( SecurePages);

foreach string key in sectionPages)
{
if ((!string.IsNullOrEmpty(key))&&(!string.IsNullOrEmpty (sectionPages.Get(key))))
{
lstPages.Add( new SecurePage {PathType = sectionPages.Get(key),Path = key});
}
}

// 循环列表以匹配列表项中具有值的路径
foreach (SecurePage page in lstPages)
{
switch (page.PathType.ToLower()。Trim())
{
case directory
< span class =code-keyword> if (path.Contains(page.Path))
{
isSecure = true ;
}
break ;
case page
if (path.ToLower()。Trim()== page.Path.ToLower()。Trim())
{
isSecure = true ;
}
break ;
默认
isSecure = false ;
break ;
}
}
}
catch (例外情况)
{
throw new 异常(ex.Message);
}

return isSecure;
}
}
#endregion
< / securepage > < / securepage >





securePage类的定义



  # regionSecurePage Class
public class SecurePage
{
string _path = ;
string _pathType = ;

public string 路径
{
get { return ._ path; }
set { this ._ path = value ; }
}

public string PathType
{
获取 {返回 ._pathType; }
set { ._ pathType = value ; }
}
}
#endregion





并在你的web.config文件中添加SecurePages部分



 <   SecurePages  >  

< add key = / Login.aspx value = page / >

< add = / WebResource.axd value = 页面 / >
< add key = / ScriptResource.axd value = page / >

< / SecurePages >





PS : - 我在CP上找到了这个解决方案并在我的项目中使用过。它完美地运作。归功于实施它的开发人员。


Pls some one tell that how to change url in browser from http://Login.aspx tp https://Login.aspx
I want to secure my page from this functionality, pls some one tell that how do this in asp.net c#

解决方案

a little research on this forum leads to:
Automatically Switch from HTTP to HTTPS[^]


Following are more links:

http://www.xdevsoftware.com/blog/post/Redirect-from-Http-to-Https-in-ASPNET.aspx[^]
http://weblogs.asp.net/scottgu/archive/2007/04/06/tip-trick-enabling-ssl-on-iis7-using-self-signed-certificates.aspx[^]


In your Global.asax file

protected void Application_BeginRequest(object sender, EventArgs e)
      {
          string path = HttpContext.Current.Request.Url.AbsolutePath;

          if (HttpContext.Current.Request.ServerVariables["HTTPS"] == "on")
          {
              if (SecurePath.IsSecure(path))
              {
                  //do nothing
              }
              else
              {
                  HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri.Replace("https://", "http://"));
                  return;
              }
          }

          if (HttpContext.Current.Request.ServerVariables["HTTPS"] != "on")
          {
              if (SecurePath.IsSecure(path))
              {
                  //Redirect to https version
                  HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.AbsoluteUri.Replace("http://", "https://"));
              }
          }


      }



Here is the definition for SecurePath Class

#region "SecurePath Class"
public class SecurePath
{

    public static bool IsSecure(string path)
    {
        List<securepage> lstPages = new List<securepage>();

        bool isSecure = false;

        try
        {
            //Fill the list of pages defined in web.config
            NameValueCollection sectionPages = (NameValueCollection)ConfigurationManager.GetSection("SecurePages");

            foreach (string key in sectionPages)
            {
                if ((!string.IsNullOrEmpty(key)) && (!string.IsNullOrEmpty(sectionPages.Get(key))))
                {
                    lstPages.Add(new SecurePage { PathType = sectionPages.Get(key), Path = key });
                }
            }

            //loop through the list to match the path with the value in the list item
            foreach (SecurePage page in lstPages)
            {
                switch (page.PathType.ToLower().Trim())
                {
                    case "directory":
                        if (path.Contains(page.Path))
                        {
                            isSecure = true;
                        }
                        break;
                    case "page":
                        if (path.ToLower().Trim() == page.Path.ToLower().Trim())
                        {
                            isSecure = true;
                        }
                        break;
                    default:
                        isSecure = false;
                        break;
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }

        return isSecure;
    }
}
#endregion
</securepage></securepage>



Definition for securePage class

#region "SecurePage Class"
   public class SecurePage
   {
       string _path = "";
       string _pathType = "";

       public string Path
       {
           get { return this._path; }
           set { this._path = value; }
       }

       public string PathType
       {
           get { return this._pathType; }
           set { this._pathType = value; }
       }
   }
   #endregion



And in your web.config file add SecurePages section

<SecurePages>

     <add key="/Login.aspx" value="page" />

   <add key="/WebResource.axd" value="page"/>
   <add key="/ScriptResource.axd" value="page"/>
     
 </SecurePages>



P.S. :- I found this solution from someone on CP and used in my project. It works perfectly. Credit goes to the developer who implemented it.


这篇关于如何在浏览器中将URL从http更改为https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆