Deployment in IIS .NET 6.0

Hello,

we have a weird problem - after deploying our project to IIS to get the userName in our "Audit" code doesn't work (ArgumentNullException)...

here is the code:

public class Auditing
    {
        private static void SetProperty(Type type, EntityEntry entityEntry, string name, object value)
        {
            var propertyInfo = type.GetProperty(name);

            if (propertyInfo != null)
            {
                entityEntry.Property(name).CurrentValue = value;
            }
        }

        public static void Audit(EntityEntry entityEntry, string userName)
        {
            var type = entityEntry.Entity.GetType();

            if (entityEntry.State == EntityState.Added)
            {
                SetProperty(type, entityEntry, "Benutzer", userName);
                SetProperty(type, entityEntry, "angelegt", DateTime.UtcNow);
            }
            else if (entityEntry.State == EntityState.Modified)
            {
                SetProperty(type, entityEntry, "Benutzer", userName);
                SetProperty(type, entityEntry, "geƤndert", DateTime.UtcNow);
                SetProperty(type, entityEntry, "gendert", DateTime.UtcNow);
            }
        }
    }

    public partial class PdbContext : Microsoft.EntityFrameworkCore.DbContext
    {
        partial void OnModelBuilding(ModelBuilder builder)
        {
            builder.Entity<PdbDetektive.Models.Pdb.Pfuscherakt>()
                .Property(p => p.Aktenzahl)
                .ValueGeneratedOnAddOrUpdate();

        }


        private readonly IHttpContextAccessor httpAccessor;

        public PdbContext(IHttpContextAccessor httpAccessor, DbContextOptions<PdbContext> options):base(options)
        {
            this.httpAccessor = httpAccessor;
        }

        public PdbContext(IHttpContextAccessor httpAccessor)
        {
            this.httpAccessor = httpAccessor;
        }

        public override int SaveChanges()
        {
            var userName = httpAccessor.HttpContext.User.FindFirst(ClaimTypes.Name).Value;

            var auditables = ChangeTracker.Entries().Where(e => e.State == EntityState.Added || e.State == EntityState.Modified);

            foreach (var auditable in auditables)
            {
                Auditing.Audit(auditable, userName);
            }

            return base.SaveChanges();
        }
    }

do you have any idea why the following line works local but not on IIS?

var userName = httpAccessor.HttpContext.User.FindFirst(ClaimTypes.Name).Value;

robert

I found this; asp.net core - HttpContext is NULL when running web app in IIS - Stack Overflow - check if web sockets are enabled in your IIS. Otherwise try using the AuthenticationStateProvider to get the current user (or inject the Radzen generated SecurityService).

if I inject SecurityService:

        public PdbContext(IHttpContextAccessor httpAccessor, SecurityService securityService, DbContextOptions<PdbContext> options):base(options)
        {
            this.httpAccessor = httpAccessor;
            this.securityService = securityService;
        }

the var userName = securityService.User.UserName; is "Anonymous"?

robert

I am not sure why this happens. Probably the same reason HttpContext does not work. Check if web sockets are installed as the StackOverflow thread says.

Websockets are installed...

Then I am not sure what the problem is. Blazor security seems to not work on your server for some reason but we are not sure why.