溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

MVC3----網(wǎng)站安全性

發(fā)布時間:2020-07-15 01:48:35 來源:網(wǎng)絡(luò) 閱讀:457 作者:1473348968 欄目:編程語言

 

=======================================一、防止跨站腳本***(XSS)

 

①: @Html.Encode("<script>alert('123')</script>")
編碼后:&lt;script&gt;alert(&#39;123&#39;)&lt;/script&gt;

②: @Html.AttributeEncode("<script>alert('123')</script>")
編碼后:&lt;script>alert(&#39;123&#39;)&lt;/script>

③: @Html.JavascriptEncode()

③: 使用antixss庫防御

 

                                                                                                               

=======================================二、防止跨站請求偽造(CSRF)

 

①: 令牌驗證(用于表單驗證)
在提交表單中加上@Html.AntiForgeryToken(),在控制器中加上[ValidateAntiforgeryToken]

②: HttpReferrer驗證(get、post)

新建一個類,繼承AuthorizeAttribute(在提交的時候驗證):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace SchoolManageDomw.Models
{
    public class IsPostedThisSiteAttribute:AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            if (filterContext.HttpContext != null)
            {
                if (filterContext.HttpContext.Request.UrlReferrer == null)
                    throw new Exception("客戶端請求的信息為空");
                if (filterContext.HttpContext.Request.UrlReferrer.Host != "localhost")//MySite.com
                    throw new Exception("不安全的請求");
            }
        }
    }
}

 控制器里使用:

        [IsPostedThisSite]
        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Home");
        }

 

 

=======================================三、cookie竊盜

 

1,cookie主要有兩種形式:
①:會話cookie:會話cookie存儲在瀏覽器的內(nèi)容中,在瀏覽器的每次請求中通過http頭傳遞
②:持久性cookie:持久性cookie存儲于計算機的實際文件中,并與會話cookie以相同的方式傳遞

 

2,使用HttpOnly阻止cookie竊盜
①:web.config文件中對所有cookie進行設(shè)置
(停止腳步對站點的cookie訪問)
<system.web>
    <httpCookies domain="" httpOnlyCookies="true" requireSSL="false"/>
</system.web>
②:在程序中為編寫的每一個cookie單獨設(shè)置
(除了服務(wù)器修改或設(shè)置cookie之外,其他一些對cookie的操作均無效)
Response.Cookies["MyCookie"].Value = "userid";
Response.Cookies["MyCookie"].HttpOnly = true;

 

=======================================四、重復(fù)提交

 

 ①:使用白名單指定允許綁定的字段
[Bind(Include="Name,Content")]
②:使用黑名單排除禁止綁定的字段
[Bind(Exclude="Price,OrderID")]
可以用在模型類中,也可以用在控制器操作參數(shù)中


例:
模型中使用:
[Bind(Include="Name,Content")]
public class product()
控制器操作參數(shù)中使用:
 public ActionResult Edit([Bind(Include = "STU_NAME,STU_MONEY")]PersonError p, string returnUrl)

 

 =======================================五、開放重定向

1,例子:

比如你是工商銀行,我是一個罪犯。
你的網(wǎng)站叫 www.icbc.com.cn,我建立了一個網(wǎng)站叫 www.icbc888.cn,我讓網(wǎng)站看起來和你的一樣。
我在論壇上發(fā)布這樣一個帖子:
快來啊,工商銀行派發(fā)紅包啦,點這里:http://www.icbc.com.cn/account/login?returnUrl=www.icbc888.cn。用戶看起來會覺得這個鏈接是工商銀行的,但是當(dāng)它點進去以后,會被跳轉(zhuǎn)到www.icbc888.cn,后面的事情你可以想象到。

 

2,控制器代碼(登錄):

returnUrl:重定向的路徑

        [HttpPost]
        public ActionResult LogOn(LogOnModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                   //做安全判斷
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("http://") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction("Index", "Home");
                    }
                }
                else
                {
                    ModelState.AddModelError("", "提供的用戶名或密碼不正確。");
                }
            }
            // 如果我們進行到這一步時某個地方出錯,則重新顯示表單
            return View(model);
        }

 

=======================================六、適當(dāng)?shù)腻e誤報告和堆棧跟蹤 

 

<customErrors mode="Off"></customErrors>
On:服務(wù)器開發(fā)的最安全選項,因為它總是隱藏錯誤提示消息
RemoteOnly:向大多數(shù)用戶展示一般的錯誤提示消息,但向擁有服務(wù)器訪問權(quán)限的用戶
展示完整的錯誤提示消息
Off:最容易受到***的選項,它向訪問網(wǎng)站的每個用戶展示詳細(xì)的錯誤提示消息

<customErrors mode="On" redirectMode="ResponseRedirect"  defaultRedirect="~/Home/Index">
<!--服務(wù)器返回404錯誤跳轉(zhuǎn)的指定頁面-->
<error redirect="~/Home/Index" statusCode="404"/>
</customErrors>

 

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI