Asp.net mvc 编辑用户删除密码-ASP.NET MVC
我正在尝试编辑我的一个用户的数据。但是,每当我编辑某些内容时,隐藏的密码也会被更改,显然设置为null,这会导致用户下次想要登录时无法登录。我知道我可能已经能够通过使用ViewModels来解决这个问题,但是我尝试不用它来解决这个问题 型号Asp.net mvc 编辑用户删除密码-ASP.NET MVC,asp.net-mvc,asp.net-identity,Asp.net Mvc,Asp.net Identity,我正在尝试编辑我的一个用户的数据。但是,每当我编辑某些内容时,隐藏的密码也会被更改,显然设置为null,这会导致用户下次想要登录时无法登录。我知道我可能已经能够通过使用ViewModels来解决这个问题,但是我尝试不用它来解决这个问题 型号 public class User : IdentityUser { [Display(Name = "First name")] public String FirstName { get; set; } [Dis
public class User : IdentityUser
{
[Display(Name = "First name")]
public String FirstName { get; set; }
[Display(Name = "Last name")]
public string LastName { get; set; }
public string Email{ get; set; }
}
请注意,User类从保存密码变量的IdentityUser扩展而来
在控制器中编辑
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "FirstName,LastName,Email,PhoneNumber")] User user)
{
if (ModelState.IsValid)
{
db.Entry(user).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(user);
}
查看以进行编辑
<div class="form-horizontal">
<h4>User</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.Id)
<div class="form-group">
@Html.LabelFor(model => model.FirstName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.FirstName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PhoneNumber, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PhoneNumber, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.PhoneNumber, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
使用者
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@Html.HiddenFor(model=>model.Id)
@LabelFor(model=>model.FirstName,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.FirstName,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.FirstName,“,new{@class=“text danger”})
@LabelFor(model=>model.LastName,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.LastName,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.LastName,“,new{@class=“text danger”})
@LabelFor(model=>model.Email,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Email,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Email,“,new{@class=“text danger”})
@LabelFor(model=>model.PhoneNumber,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.PhoneNumber,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.PhoneNumber,“,new{@class=“text danger”})
}
据我所知,编辑方法中的Bind参数可以是白名单,也可以是黑名单,用于编辑变量。因此,出于这个原因,我删除了绑定中用户不应编辑的所有值。这里有几个问题。首先,首先不要向用户显示这些字段。我无法想象为什么用户能够编辑他们的“锁定”状态,或者他们的散列密码。仅在UI中包含用户实际应修改的字段。见鬼,甚至这上面都写着可怕的想法:
@Html.HiddenFor(model => model.Id)
您不仅允许用户编辑有关其用户记录的所有内容,还允许用户指定另一个要编辑的用户记录。因此,系统中的任何用户都可以完全编辑系统中的任何其他用户
希望您看到这是一件多么糟糕的事情:)
现在,您可以(通常必须)在隐藏字段中包含标识符。上述问题主要是由于您正在做的其他事情造成的:
db.Entry(user).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
您完全且隐式地相信用户发送给您的任何内容都是数据库的完整、正确和完整的记录。并用用户发送给您的任何内容替换现有记录。那是。。。不太好
这种方法适用于某些模型,但肯定不适用于敏感模型,如用户安全数据
取而代之的是,获取现有记录并只编辑必要的字段。大概是这样的:
var existingUser = db.Users.Single(u => u.Id == currentUserId);
existingUser.FirstName = user.FirstName;
existingUser.LastName = user.LastName;
// etc.
db.SaveChanges();
请注意,我使用了一个名为currentUserId
的未定义变量。不要使用model.Id
,因为这同样允许用户指定要编辑的其他用户。通过当前登录会话确定当前用户ID,而不是通过表单中发送的内容。但是,您当前识别您的用户。(User.Identity
?)
简言之
- 仅允许用户查看/编辑他们允许的内容
- 在“保存”操作中验证是否允许用户编辑该数据(决不要认为用户之前打开了页面就必须允许他们编辑该数据)
- 只更新该操作中要更新的值,而不只是批量替换敏感数据的整个记录