Ejemplo n.º 1
0
 public HttpAuditSubject(IHttpContextAccessor accessor, AuditHttpSubjectOptions options)
 {
     SubjectIdentifier     = accessor.HttpContext.User.FindFirst(options.SubjectIdentifierClaim)?.Value;
     SubjectName           = accessor.HttpContext.User.FindFirst(options.SubjectNameClaim)?.Value;
     SubjectAdditionalData = new
     {
         RemoteIpAddress = accessor.HttpContext.Connection?.RemoteIpAddress?.ToString(),
         LocalIpAddress  = accessor.HttpContext.Connection?.LocalIpAddress?.ToString(),
         Claims          = accessor.HttpContext.User.Claims?.Select(x => new { x.Type, x.Value })
     };
 }
        /// <summary>
        /// Add audit default data - subject and action
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="subjectOptions"></param>
        /// <param name="actionOptions"></param>
        /// <returns></returns>
        public static IAuditLoggingBuilder AddDefaultHttpEventData(this IAuditLoggingBuilder builder, Action <AuditHttpSubjectOptions> subjectOptions = default, Action <AuditHttpActionOptions> actionOptions = default)
        {
            var auditHttpSubjectOptions = new AuditHttpSubjectOptions();

            subjectOptions?.Invoke(auditHttpSubjectOptions);
            builder.Services.AddSingleton(auditHttpSubjectOptions);

            var auditHttpActionOptions = new AuditHttpActionOptions();

            actionOptions?.Invoke(auditHttpActionOptions);
            builder.Services.AddSingleton(auditHttpActionOptions);

            builder.Services.AddTransient <IAuditSubject, HttpAuditSubject>();
            builder.Services.AddTransient <IAuditAction, HttpAuditAction>();

            return(builder);
        }