当您使用sqlcheck和cookie来验证用户以存储信息时,我现在使用“标准”登录。 (如代码示例所示)。现在我想在这里存储一些关于客户的更多信息。例如,如果他以管理员身份登录,他选择了哪个部门进行身份验证等。最好的办法是什么? (会话,缓存,存储用户身份信息)
一直在四处寻找但没有找到关于MVC3框架的安全性和标准的最佳答案。由于我是MVC的新手,我想直接“挖掘”到正确的habbits ... anny建议/意见赞赏。
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(LoginModel model)
{
if (ModelState.IsValid)
{
if (model.Username == "qwe" && model.Password == "qwe")// Proper
sql authentication to be implemented here
{
FormsAuthentication.SetAuthCookie(model.Username, false);
// True or false determents if its a persistent coockie or not
return RedirectToAction("Index", "Home");
}
{
ModelState.AddModelError("", "Wrong login information");
}
}
return View();
}
答案 0 :(得分:0)
您使用的是aspnet sql会员提供商吗?如果是这样,您可以将[Authorize]属性与角色一起使用。这样,您就可以在站点角色上锁定站点,例如admin。您可以扩展成员资格提供程序以使每个用户具有自定义属性很难说这是否足够。我想我们需要知道您要添加的所有用户属性以及它们提供的功能。例如,如果他们在某个部门,他们可以做什么或不做什么。
答案 1 :(得分:0)
对于authenticiticaiton,您应该使用基于IPrincipal的东西,例如FormsAuthentication或WindowsAuthentication。您可以使用[Authorize]
属性来阻止访问,并控制哪些角色可以访问该方法(即[Authorize(Roles = "Administrator")]
)。
要获得更细粒度的身份验证,请检查User.Identity.IsAuthenticated
或User.IsInRole()
方法(即
@if (User.IsInRole("Administrator")) {
<your code for only an administrator>
}
如果需要,您可以使用相同的技术查找部门的角色,或使用User.Identity.Name
根据用户ID查找部门。