ASP.NET MVC 3中默认的强类型编辑页面通常公开实体的所有字段.虽然这通常是可行的,但某些领域存在安全风险.例如,简化的杂志订阅实体可能如下所示:
public void Subscription() {
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public DateTime SubscribedThru { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
例如,如果我提供编辑页面以允许用户更改自己的地址,那么包含该SubscribedThru字段会带来安全风险,因为知识渊博的恶意用户可以通过伪造日期为自己提供10年免费订阅(即使我使用@Html.HiddenFor(model => model.SubscribedThru).因此,我不会在编辑页面html(通过razor)中以任何方式包含该字段.
我认为答案可能是防止SubscribedThru在控制器中对Edit方法进行绑定尝试,例如:
[HttpPost]
public ActionResult Edit([Bind(Exclude="SubscribedThru")] Subscription subscription) {
if (ModelState.IsValid) {
db.Entry(subscription).State = EntityState.Modified; …Run Code Online (Sandbox Code Playgroud)