For the first part of your question, authorization based on a claim, I already answered this question to this similar question . And I'm not going to repeat here.
But for your other rules, such as products that are only editable by the owner. You can write a separate AuthorizeAttribute for each rule and apply them in your actions, considering this as a simple example:
using Microsoft.AspNet.Identity; public class OwnerAuthorizeAttribute : AuthorizeAttribute { private string _keyName; public bool IsPost { get; set; } public OwnerAuthorizeAttribute(string keyName) { _keyName = keyName; } protected override bool AuthorizeCore(HttpContextBase httpContext) {
You can repeat the same pattern with different rules.
And you can just apply your custom attributes to your actions:
[OwnerAuthorize("id")] public ActionResult Edit(int id) {
Obviously, you can apply more than one AuthorizeAttribute to your actions. In this case, all of them should return true .
[ClaimsAuthorize("Product", "EDIT")] [OwnerAuthorize("id")] [YetOtherAuthorize] public ActionResult MyFancyAction(int id) { }
Sam Farajpour Ghamari Aug 6 '15 at 8:54 2015-08-06 08:54
source share