In the web api that i am building there some controllers which have actions which receives a jwttoken from React as a parameter an example of this shown below.

```
public async Task<IActionResult> post(string token)
        {
            if (!string.IsNullOrEmpty(token))
            {
                IJsonSerializer serializer = new JsonNetSerializer();
                IDateTimeProvider provider = new UtcDateTimeProvider();
                IJwtValidator validator = new JwtValidator(serializer, provider);
                IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
                IJwtDecoder decoder = new JwtDecoder(serializer, validator, urlEncoder);

                var jsonDecoded = decoder.Decode(token);
                var jwtObject = JsonConvert.DeserializeObject<JwtToken>(jsonDecoded);
//do some business logic
}

```

Lot of method have the same code being duplicated to check the token by decoding it and then proceed further with there own responsibility.

I have create a authentication filter 

```
public class AuthenticateFilter : Attribute,IActionFilter
    {
        public void OnActionExecuted(ActionExecutedContext context)
        {
            throw new NotImplementedException();
        }

        public void OnActionExecuting(ActionExecutingContext context)
        {
            IJsonSerializer serializer = new JsonNetSerializer();
            IDateTimeProvider provider = new UtcDateTimeProvider();
            IJwtValidator validator = new JwtValidator(serializer, provider);
            IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
            IJwtDecoder decoder = new JwtDecoder(serializer, validator, urlEncoder);

            var token = context.HttpContext.Request.QueryString.HasValue ? context.HttpContext.Request.QueryString.Value.Substring(7):String.Empty;
            var jsonDecoded = decoder.Decode(token);
            var jwtObject = JsonConvert.DeserializeObject<JwtToken>(jsonDecoded);
        }
    }

```

I have registered this filter in startup.cs

```
services.AddMvc(
                options =>
                {
                    options.Filters.Add(new AuthenticateFilter());
                    options.Filters.Add(typeof(AuthenticateFilter));
                }

                ).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

```

I am using a library to decode the token. 
<https://github.com/jwt-dotnet/jwt>

after the decode of the token in the filter I am de-serializing the json string into a .net class which is shown below . 

```
public class JwtToken
    {
        [JsonProperty("sub")]
        public Guid Sub { get; set; }

        [JsonProperty("aud")]
        public string Aud { get; set; }
}

```

how can I have access to the object 

```
 var jwtObject = JsonConvert.DeserializeObject<JwtToken>(jsonDecoded);

```

in all the controller action where i decorate the attribute .

On a side note Are there any better way to solve it ,suggestions are welcome.