它是使用 DataProtection 保护 Asp.Net Core MVC 中的表单模型的好方法吗
Is it good way for protecting the form model in Asp.Net Core MVC using DataProtection
我已经尝试创建 Asp.Net 核心 MVC 应用程序,我想在其中保护表单模型,尤其是 Id。
我发现了 DataProtection 与方法的可能性 - Protect
和 Unprotect
字符串。
我用过这个实现:
public class HomeController : Controller
{
readonly IDataProtector _protector;
private readonly IUserRepository _userRepository;
public HomeController(IDataProtectionProvider provider, IUserRepository userRepository)
{
_protector = provider.CreateProtector("DataProtectionDemo.Controllers.HomeController");
_userRepository = userRepository;
}
[HttpGet]
public async Task<IActionResult> Index(int id)
{
var user = await _userRepository.GetUserDetail(id);
user.Id = _protector.Protect(user.Id);
return View(user);
}
[HttpPost]
public async Task<IActionResult> Index(UserViewModel model)
{
try
{
model.Id = _protector.Unprotect(model.Id);
await _userRepository.SaveUser(model);
return RedirectToAction(nameof(Index));
}
catch (Exception e)
{
model.Error = e.Message;
return View(model);
}
}
在这种情况下,我想用加密字符串保护隐藏字段中的 UserId,但我不知道这种使用 Dataprotection 的方法是否正确。我知道围绕授权策略的可能性,下一步可能是检查用户权限,但我想知道这种额外的方法可以提供更好的保护。
保护表单模型的方法好吗?
我已经在 Github 的 DataProtection
主题中打开了这个问题:
https://github.com/aspnet/DataProtection/issues/278
答案是这个解决方案应该可以正常工作。
我已经尝试创建 Asp.Net 核心 MVC 应用程序,我想在其中保护表单模型,尤其是 Id。
我发现了 DataProtection 与方法的可能性 - Protect
和 Unprotect
字符串。
我用过这个实现:
public class HomeController : Controller
{
readonly IDataProtector _protector;
private readonly IUserRepository _userRepository;
public HomeController(IDataProtectionProvider provider, IUserRepository userRepository)
{
_protector = provider.CreateProtector("DataProtectionDemo.Controllers.HomeController");
_userRepository = userRepository;
}
[HttpGet]
public async Task<IActionResult> Index(int id)
{
var user = await _userRepository.GetUserDetail(id);
user.Id = _protector.Protect(user.Id);
return View(user);
}
[HttpPost]
public async Task<IActionResult> Index(UserViewModel model)
{
try
{
model.Id = _protector.Unprotect(model.Id);
await _userRepository.SaveUser(model);
return RedirectToAction(nameof(Index));
}
catch (Exception e)
{
model.Error = e.Message;
return View(model);
}
}
在这种情况下,我想用加密字符串保护隐藏字段中的 UserId,但我不知道这种使用 Dataprotection 的方法是否正确。我知道围绕授权策略的可能性,下一步可能是检查用户权限,但我想知道这种额外的方法可以提供更好的保护。
保护表单模型的方法好吗?
我已经在 Github 的 DataProtection
主题中打开了这个问题:
https://github.com/aspnet/DataProtection/issues/278
答案是这个解决方案应该可以正常工作。