1

I have created a class to validate my own token. Within this class I need to use a service, which I have previously added in the startup.cs with services.AddScoped. how can I do it ?

x.SecurityTokenValidators.Add(new DynamicKeyJwtValidationHandler());

Inside the DynamicKeyJwtValidationHandler class I need to use a service.

public class DynamicKeyJwtValidationHandler : JwtSecurityTokenHandler, ISecurityTokenValidator
    {
        public SecurityKey GetKeyForClaimedId(string claimedId)
        {
            throw new NotImplementedException();
        }

        public override ClaimsPrincipal ValidateToken(string token, TokenValidationParameters validationParameters, out SecurityToken validatedToken)
        {
            ClaimsPrincipal cp = new ClaimsPrincipal();
            validatedToken = null;
            try
            {
                cp = base.ValidateToken(token, validationParameters, out validatedToken);
            } catch (Exception) {}

            return cp;
        }
    }
ArunPratap
  • 4,816
  • 7
  • 25
  • 43
vidgarga
  • 320
  • 1
  • 3
  • 8

1 Answers1

1

Use dependency injection in the constructor of the class

public class DynamicKeyJwtValidationHandler : JwtSecurityTokenHandler, ISecurityTokenValidator
    {
        private readonly YourService _service
        public DynamicKeyJwtValidationHandler(YourService service){
             _service = service
        }
        public SecurityKey GetKeyForClaimedId(string claimedId)
        {
            throw new NotImplementedException();
        }

        public override ClaimsPrincipal ValidateToken(string token, TokenValidationParameters validationParameters, out SecurityToken validatedToken)
        {
            ClaimsPrincipal cp = new ClaimsPrincipal();
            validatedToken = null;
            try
            {
                cp = base.ValidateToken(token, validationParameters, out validatedToken);
            } catch (Exception) {}

            return cp;
        }
    }
  • no, because then I can't do new DynamicKeyJwtValidationHandler() because constructor expects a service – vidgarga Dec 17 '19 at 11:08
  • @vidgarga, you should be injecting your DynamicKeyJwtValidationHandler too, and not call its constructor directly using new. So DynamicKeyJwtValidationHandler should be a service too. – Polyfun Dec 17 '19 at 11:18
  • Like Polyfun said ! However if you do not want to follow trough with this pattern you can just instantiate the service in the validationhandler class or pass it when calling `new DynamicKeyJwtValidationHandler()` . I would highly recommend the proper injecting pattern tho ! – Samuel Middendorp Dec 17 '19 at 11:23
  • @Polyfun Can you write a little example? – vidgarga Dec 17 '19 at 11:49
  • @vidgarga As DynamicKeyJwtValidationHandler implements ISecurityTokenValidator, then it should probably be added as a ISecurityTokenValidator service. This post might help https://stackoverflow.com/questions/47138849/how-to-correctly-get-dependent-scoped-services-from-isecuritytokenvalidator. – Polyfun Dec 17 '19 at 13:54