1

已经问过类似的问题,但我想知道是否可以简化解决方案。您可能已经注意到,我想确保登录的用户只能访问他的数据。我在 mvc3 中使用自定义成员资格提供程序。

[Authorize(Users=httpContext.User.Identity.Name)]
public ActionResult UserArea()
{}

谢谢

4

1 回答 1

0

创建一个新属性并覆盖该AuthorizeCore方法:

public class CustomAuthorizeAttribute : AuthorizeAttribute
{
   protected override bool AuthorizeCore(HttpContext.User user)
   {
     // Generally authenticated to the site
     if (!httpContext.User.Identity.IsAuthenticated) {
        return false;
     }

    return true;
   }
}

然后在你的控制器上做:

[CustomAuthorize(httpContext.User]
public ActionResult MyControllerAction()
{
    return View();
}

http://code.commongroove.com/2012/04/20/asp-net-mvc-simple-custom-authorization-by-inheriting-from-the-authorizeattribute/

于 2013-02-15T08:25:41.920 回答