6

Hi I want to pass parameters to the custom policy in asp.net core. As i know so far i can only do

[Authorize(Policy="Over21")]
My Action(){
}

but i want to do

[Authorize(Policy="Over21",Para="AnotherValue")]
My Action(){
    }

so i decided to extend Authorize attribute and then do this.

public class MyAuthorizationAttribute : AuthorizeAttribute
    {
        public string Permission;    
        public MyAuthorizationAttribute(string policy, string permission)
            : base(policy)
        {
            Permission = permission;
        } 

    }

Then do this

[MyAuthorization("MyPolicy","CanDoPermission")]
My Action(){
    }

The custom authorization handler is still called when i debug and so now i want a way to get the permission property from the AuthorizationHandlerContext context. I realize that i can find this property when i drill down during debug

context->Resource->Filters

Among this list i find all objects of type AuthorizeFilter and further drill down to AuthorizeData->Resultview this also is a list that contains my MyAuthorization object. I use a debut tool to do this but i dont know how to get MyAuthorization instance that applies to the specific action being called?

This is the furthest i get.

var mvcContext = context.Resource as AuthorizationFilterContext;

            foreach (var filterMetadata in mvcContext.Filters)
            {
                if (filterMetadata.GetType().Name != "AuthorizeFilter") continue;

                var claimAttribute = filterMetadata as AuthorizeFilter;
                if (claimAttribute != null)
                {
                    var data = claimAttribute.AuthorizeData;
                }

Am not sure what to do next to get this object. I am hoping there is an easier way to get these values from the context.

0

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy