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 に答える