我在一个应用程序中工作,需要验证请求是否来自经过身份验证的用户。因为我根据用户Guid进行了大量负载。


public ActionResult ManageEmployee()
{
      var loadedEmp = GetLoadedEmp(GetLoggedUserGuid());
      return View("Employee/Manage", loadedEmp);
}

我的问题是,我是否需要执行“已验证用户身份验证”
public ActionResult ManageEmployee()
{
 if (!User.Identity.IsAuthenticated)
 {
        return View("Account/LogOn");
  }

  var loadedEmp = GetLoadedEmp(GetLoggedUserGuid());
  return View("Employee/Manage", loadedEmp);
}

在每个ActionResult函数上,还是存在野兽方法或集中式解决方案。

谢谢

最佳答案

在 Controller 中的Action上使用 AuthorizeAttribute ,要求用户登录才能执行该Action:

[Authorize]
public ActionResult ManageEmployee()
{
    // This code will only execute if the user is Authenticated
    var loadedEmp = GetLoadedEmp(GetLoggedUserGuid());
    return View("Employee/Manage", loadedEmp);
}

使用该属性时,如果用户未登录,则该用户将自动重定向到登录页面(只要您的应用配置正确)。

关于asp.net-mvc - 在ASP.NET MVC中验证用户身份验证的最佳位置在哪里,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4890758/

10-12 02:10