Asp.Net 核心扩展身份
Asp.Net Core extending Identity
我有一个比语法更高级的问题。
简而言之,我正在构建一个 Asp.Net 核心 Web 应用程序(使用 MVC,使用内置的用户身份验证)。
我正在扩展现有的身份实体以添加我们需要的属性(Identity/ApplicationUser 对象中尚不存在的属性)。
最终,我需要让管理员 can/will 进入一个页面(视图)以设置新用户或另一个页面(视图),管理员可以在其中修改任何属性(除了password/login) 现有用户。
现在我正竭尽全力使它工作(我修改了生成的 Register 视图以用作 "set up new users" 视图并使所有这些工作正常),我现在正在工作在编辑视图上,我意识到我可能走错了路。
按照这种方式,我担心我可能需要想出一种方法来拥有一个不会覆盖加密密码的编辑视图,或者要求管理员在需要时随时更改用户密码更改其他内容(例如,更改他们的部门,或授予他们访问网站部分内容的权限)。
所以现在我想知道是否有更好的方法我应该去完成这个。
建议?推荐?
现在重新开始这部分还不算太晚。
您需要确保您不是 overposting。使用 TryUpdateModel 更新带有发布值的字段是一种安全最佳实践,因为它可以防止过度发布。
For example, suppose the Student entity includes a Secret property
that this web page shouldn't update or add. Even if the app doesn't
have a Secret field on the create/update Razor Page, a hacker could
set the Secret value by overposting. A hacker could use a tool such as
Fiddler, or write some JavaScript, to post a Secret form value. The
original code doesn't limit the fields that the model binder uses when
it creates a Student instance. Whatever value the hacker specified for the Secret form field is
updated in the DB.
使用 TryUpdateModel 防止过度发布的示例:
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
return Page();
var emptyStudent = new Student();
if (await TryUpdateModelAsync<Student>(
emptyStudent,
"student", // Prefix for form value.
s => s.FirstMidName, s => s.LastName, s => s.EnrollmentDate))
{
_context.Student.Add(emptyStudent);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
return null;
}
我个人从不使用实体对象,而是使用 ViewModel。最肯定的是,当涉及到身份时。首先要保持关注点分开。作为好处,事情会变得容易得多。虽然像过度发布这样的事情不太可能发生。
对于管理员,您可以使用替代的 TryUpdateModel。但我不会将 CRUD 用于 Identity。而是使用 UserManager 等的现有功能。
我有一个比语法更高级的问题。
简而言之,我正在构建一个 Asp.Net 核心 Web 应用程序(使用 MVC,使用内置的用户身份验证)。
我正在扩展现有的身份实体以添加我们需要的属性(Identity/ApplicationUser 对象中尚不存在的属性)。
最终,我需要让管理员 can/will 进入一个页面(视图)以设置新用户或另一个页面(视图),管理员可以在其中修改任何属性(除了password/login) 现有用户。
现在我正竭尽全力使它工作(我修改了生成的 Register 视图以用作 "set up new users" 视图并使所有这些工作正常),我现在正在工作在编辑视图上,我意识到我可能走错了路。
按照这种方式,我担心我可能需要想出一种方法来拥有一个不会覆盖加密密码的编辑视图,或者要求管理员在需要时随时更改用户密码更改其他内容(例如,更改他们的部门,或授予他们访问网站部分内容的权限)。
所以现在我想知道是否有更好的方法我应该去完成这个。
建议?推荐?
现在重新开始这部分还不算太晚。
您需要确保您不是 overposting。使用 TryUpdateModel 更新带有发布值的字段是一种安全最佳实践,因为它可以防止过度发布。
For example, suppose the Student entity includes a Secret property that this web page shouldn't update or add. Even if the app doesn't have a Secret field on the create/update Razor Page, a hacker could set the Secret value by overposting. A hacker could use a tool such as Fiddler, or write some JavaScript, to post a Secret form value. The original code doesn't limit the fields that the model binder uses when it creates a Student instance. Whatever value the hacker specified for the Secret form field is updated in the DB.
使用 TryUpdateModel 防止过度发布的示例:
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
return Page();
var emptyStudent = new Student();
if (await TryUpdateModelAsync<Student>(
emptyStudent,
"student", // Prefix for form value.
s => s.FirstMidName, s => s.LastName, s => s.EnrollmentDate))
{
_context.Student.Add(emptyStudent);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
return null;
}
我个人从不使用实体对象,而是使用 ViewModel。最肯定的是,当涉及到身份时。首先要保持关注点分开。作为好处,事情会变得容易得多。虽然像过度发布这样的事情不太可能发生。
对于管理员,您可以使用替代的 TryUpdateModel。但我不会将 CRUD 用于 Identity。而是使用 UserManager 等的现有功能。