asp.net cookie操作(防止cookie 欺騙 )

來源:互聯網
上載者:User

protected void page_load(object sender, eventargs e)
        {
                if (request.cookies["username"] == null)
                {
                    httpcookie mycookie = new httpcookie("username");
                    mycookie.values.add("prodid", "");
                    mycookie.expires = datetime.now.adddays(1);
                    response.cookies.add(mycookie);
                }
        }

這是插入cookie的代碼

 public void addcookie(string cookievalue)
        {
           
            httpcookie cookie = new httpcookie("username");
            if (request.cookies["username"].values["prodid"] != "")
            {
                string myvalue = "";
                myvalue = request.cookies["username"].values["prodid"].tostring();
                myvalue = cookievalue + "," + myvalue;
                cookie.values.add("prodid", myvalue);
                cookie.expires = datetime.now.adddays(1);
                response.cookies.add(cookie);
            }
            else
            {
                cookie.values.add("prodid", cookievalue);
                cookie.expires = datetime.now.adddays(1);
                response.cookies.add(cookie);
            }
        }

最後是刪除cookie的方法

        protected void button2_click(object sender, eventargs e)
        {
            httpcookie mycookie = new httpcookie("username");
            mycookie.expires = datetime.now.adddays(-1d);
            response.cookies.add(mycookie);
        }

防止cookie 欺騙

按照瀏覽器的約定,只有來自同一網域名稱的cookie才可以讀寫,而cookie只是瀏覽器的,對通訊協議無影響,所以要進行cookie欺騙可以有多種途徑,最簡單的方法自己建立一個網站,在c:windowssystem32driversetchosts 中把這個自己建立的網站制定成想要欺騙的網域名稱,cookie寫入以後再把hosts的值改回來,這樣這個本地的網站的cookie就可以拋到你想要入侵的網域名稱下

 

public void logined(modeluser model)
  {
      int outtime = getloginouttime();
      httpcontext.current.response.cookies["username"].value=model.user_name;
      httpcontext.current.response.cookies["username_check"].value =dessecurity.desencrypt(model.user_name);
      httpcontext.current.response.cookies["username"].expires = datetime.now.addminutes(outtime);
      httpcontext.current.response.cookies["username_check"].expires = datetime.now.addminutes(outtime);
      setusermodel(model);
  }


可以看到儲存了兩個cookie值,都是使用者名稱,一個是加密的一個是未加密的

驗證是否登陸的代碼如下:

public bool islogin()
{
    bool islogin = false;
    if (httpcontext.current.request.cookies["username"] != null)
    {
        if (httpcontext.current.request.cookies["username_check"] != null)
        {
            string username = httpcontext.current.request.cookies["username"].value;
            string usernamecheck = httpcontext.current.request.cookies["username_check"].value;
            if (username == dessecurity.desdecrypt(usernamecheck))
                islogin = true;
        }
    }
    return islogin;
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.