c# - Creating a new AuthorizationHandler/IAuthorizationRequirement that uses a service -


i trying create new authorization requirement, must use 1 of services declare in configureservices, , have no idea how pass service new requirement in same method declaring service.

public class newrequirement: authorizationhandler<newrequirement>, iauthorizationrequirement {     private irepository _repository;      public newrequirement(irepository repository)     {         _repository = repository;     }      protected override void handle(authorizationcontext context, newrequirement requirement)     {         //code uses _repository here     } } 

this works fine, when try add policy in startup.cs this:

    public void configureservices(iservicecollection services)     {         ...          services.addsingleton<repositorycontext>();         services.addscoped<irepository, repository>();          services.configure<authorizationoptions>(options =>         {             options.addpolicy("newrequirement", policy => policy.requirements.add(new newrequirement()));         });     } 

i error

error cs7036
there no argument given corresponds required formal parameter 'repository' of 'extraprojectrequirements.newrequirement(irepository)'
reception.dnx 4.5.1

i think need pass irepository new policy have no idea how in configureservices.

you passing handler requirement, wrong. iauthorizationrequirement , authorizationhandler<newrequirement> need 2 distinct classes. iauthorizationrequirement marker interface w/o mandatory properties or methods, there accidentally adding arbitrary classes requirements collection ;)

the iauthorizationrequirement contain pure data (reads: no services, no dependencies need injected) required requirement, handler validate it. see @blowdart example of over18requirement , it's handler official documentation.

handlers allowed have dependencies injected.

examples documentation future readers (in case link becomes unavailable).

public class minimumagerequirement : iauthorizationrequirement {     public minimumagerequirement(int age)     {         minimumage = age;     }      protected int minimumage { get; set; } }  public class minimumagehandler : authorizationhandler<minimumagerequirement> {     protected override void handle(authorizationcontext context, minimumagerequirement requirement)     {         if (!context.user.hasclaim(c => c.type == claimtypes.dateofbirth &&                                    c.issuer == "http://contoso.com"))         {             return;         }          var dateofbirth = convert.todatetime(context.user.findfirst(             c => c.type == claimtypes.dateofbirth && c.issuer == "http://contoso.com").value);          int calculatedage = datetime.today.year - dateofbirth.year;         if (dateofbirth > datetime.today.addyears(-calculatedage))         {             calculatedage--;         }          if (calculatedage >= requirement.minimumage)         {             context.succeed(requirement);         }     } } 

Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo