Tany
Tany

Reputation: 1322

Adding trace logs when post authorization using UseJwtBearerAuthentication

I have a asp.net mvc website with auth configured as below

app.UseJwtBearerAuthentication(
new JwtBearerAuthenticationOptions
{
    TokenValidationParameters = new TokenValidationParameters()
    {
        AudienceValidator = ValidateAudience,
        ValidIssuer = GlobalUri,
        SaveSigninToken = true,
        IssuerSigningKeyResolver = await IssuerSigningKeyResolverAsync(),

    }
});

My question is I want to add trace logs when a user is authenticated or otherwise.

Unfortunately, I don't see a hook where I can do some post processing after auth in owin pipeline. Or is there an other way ?

Upvotes: 0

Views: 205

Answers (1)

Victor Hugo Terceros
Victor Hugo Terceros

Reputation: 3169

I think you have to use filters in order to log the user activity, you can create a filter this way

 public class UserActivityFilter : ActionFilterAttribute, IActionFilter
    {

        void IActionFilter.OnActionExecuting(ActionExecutingContext actionExecutedContext)
        {
            Log(actionExecutedContext);
            base.OnActionExecuting(actionExecutedContext);
        }

        private void Log(ActionExecutingContext filterContext)
        {
            //Your logic log here
        }
    }

Then register your filter in App_Start\FilterConfig.cs

public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new UserActivityFilter());
        }
    }

Upvotes: 1

Related Questions