# User Information on Entities

**URL:** <https://forum.radzen.com/t/user-information-on-entities/1016>\
**Category:** Angular\
**Created:** [March 21, 2019, 11:31am UTC](https://forum.radzen.com/t/user-information-on-entities/1016 "2019-03-21T11:31:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Pitchford](https://forum.radzen.com/user_avatar/forum.radzen.com/paul_pitchford/32/687_2.png) [@Paul\_Pitchford](https://forum.radzen.com/u/Paul_Pitchford)\
**Post date:** [March 21, 2019, 11:31am UTC](https://forum.radzen.com/t/user-information-on-entities/1016/1 "2019-03-21T11:31:02Z")

</div>

Hello,

In lightswitch when the integrated security was used, all the entities in the database would get the following properties:

- Created (datetime)

- Createdby (nvarchar)

- Modified (datetime)

- ModifiedBy (datetime)

The system would automatically date stamp and record the username in these properties when a record was created or updated.

Is there a best practice for this in Radzen or am I missing something where this may be implemented automatically? I have created the properties on my entities, I'd just like to implement the code in one location across all entities rather than having to handle each one.

Thanks.

---

<div class="post-metadata">

**Author:** ![korchev](https://forum.radzen.com/user_avatar/forum.radzen.com/korchev/32/4_2.png) [@korchev](https://forum.radzen.com/u/korchev)\
**Post date:** [March 21, 2019, 1:13pm UTC](https://forum.radzen.com/t/user-information-on-entities/1016/2 "2019-03-21T13:13:55Z")

</div>

This isn't automatically done at the moment. As a first step we will prepare a sample application based on [this](https://stackoverflow.com/questions/49202615/audit-trail-with-entity-framework) approach and post it here.

---

<div class="post-metadata">

**Author:** ![Paul\_Pitchford](https://forum.radzen.com/user_avatar/forum.radzen.com/paul_pitchford/32/687_2.png) [@Paul\_Pitchford](https://forum.radzen.com/u/Paul_Pitchford)\
**Post date:** [March 21, 2019, 1:45pm UTC](https://forum.radzen.com/t/user-information-on-entities/1016/3 "2019-03-21T13:45:09Z")

</div>

Thank you. That would be really helpful.

---

<div class="post-metadata">

**Author:** ![mumfie](https://forum.radzen.com/letter_avatar_proxy/v4/letter/m/b4bc9f/32.png) [@mumfie](https://forum.radzen.com/u/mumfie)\
**Post date:** [March 21, 2019, 1:47pm UTC](https://forum.radzen.com/t/user-information-on-entities/1016/4 "2019-03-21T13:47:48Z")

</div>

We were able to implement this with custom partial classes that implement a custom interface class IEntityAudit and override the context SaveChanges as per link (1) above.

```
 interface IEntityAudit
    {
        [Key]
        Guid Id { get; set; }
        DateTime CreatedAt { get; set; }
        DateTime LastUpdatedAt { get; set; }  
        [StringLength(20)]
        string LastUpdatedBy { get; set; }             
    }
```

---

<div class="post-metadata">

**Author:** ![Paul\_Pitchford](https://forum.radzen.com/user_avatar/forum.radzen.com/paul_pitchford/32/687_2.png) [@Paul\_Pitchford](https://forum.radzen.com/u/Paul_Pitchford)\
**Post date:** [March 21, 2019, 1:55pm UTC](https://forum.radzen.com/t/user-information-on-entities/1016/5 "2019-03-21T13:55:03Z")

</div>

> We were able to implement this with custom partial classes that implement a custom interface class IEntityAudit and override the context SaveChanges as per link (1) above.

Thank you. I kinda get how this will be implemented and hope to see the example from Atanas to clarify it.

I do have another question though. If you override the SaveChanges function on the context and update the data source by inferring from schema again, will it overwrite any code you implement on the context?

Thanks.

---

<div class="post-metadata">

**Author:** ![korchev](https://forum.radzen.com/user_avatar/forum.radzen.com/korchev/32/4_2.png) [@korchev](https://forum.radzen.com/u/korchev)\
**Post date:** [March 21, 2019, 1:57pm UTC](https://forum.radzen.com/t/user-information-on-entities/1016/6 "2019-03-21T13:57:13Z")

</div>

It will not if you use a partial class as it lives in a different file.

---

<div class="post-metadata">

**Author:** ![Paul\_Pitchford](https://forum.radzen.com/user_avatar/forum.radzen.com/paul_pitchford/32/687_2.png) [@Paul\_Pitchford](https://forum.radzen.com/u/Paul_Pitchford)\
**Post date:** [March 21, 2019, 3:34pm UTC](https://forum.radzen.com/t/user-information-on-entities/1016/7 "2019-03-21T15:34:13Z")

</div>

Thanks Atanas, my basic implementation of what I've learnt today seems to fail to expose the username.

I have implemented the partial class for overriding the Saving function and can access the properties to update the modified and creation dates but I can't seem to figure how to expose the username for the CreatedBy and ModifiedBy properties.

I look forward to your demo. Thanks.

---

<div class="post-metadata">

**Author:** ![korchev](https://forum.radzen.com/user_avatar/forum.radzen.com/korchev/32/4_2.png) [@korchev](https://forum.radzen.com/u/korchev)\
**Post date:** [March 21, 2019, 3:51pm UTC](https://forum.radzen.com/t/user-information-on-entities/1016/8 "2019-03-21T15:51:29Z")

</div>

[This](https://www.radzen.com/documentation/get-current-user/) tutorial show how to get the current user name.

---

<div class="post-metadata">

**Author:** ![Paul\_Pitchford](https://forum.radzen.com/user_avatar/forum.radzen.com/paul_pitchford/32/687_2.png) [@Paul\_Pitchford](https://forum.radzen.com/u/Paul_Pitchford)\
**Post date:** [March 21, 2019, 3:56pm UTC](https://forum.radzen.com/t/user-information-on-entities/1016/9 "2019-03-21T15:56:53Z")

</div>

Yeah I'd seen that but in the SaveChanges function I can't seem to figure out how to access the ```  
HttpContext. I can see how that would work from the controller but I was trying to handle all this from that override function.

---

<div class="post-metadata">

**Author:** ![korchev](https://forum.radzen.com/user_avatar/forum.radzen.com/korchev/32/4_2.png) [@korchev](https://forum.radzen.com/u/korchev)\
**Post date:** [March 21, 2019, 4:02pm UTC](https://forum.radzen.com/t/user-information-on-entities/1016/10 "2019-03-21T16:02:14Z")

</div>

You are right. There is no easy way to get that in the EF context class at the moment. Will have to inject it during code generation. Right now you can get it only from the Controller classes. This would allow you to set the CreatedBy/UpdatedBy fields but you will have to create a partial class for every controller which is less than ideal. We just have to release a new Radzen version that takes care of this.

---

<div class="post-metadata">

**Author:** ![Paul\_Pitchford](https://forum.radzen.com/user_avatar/forum.radzen.com/paul_pitchford/32/687_2.png) [@Paul\_Pitchford](https://forum.radzen.com/u/Paul_Pitchford)\
**Post date:** [March 22, 2019, 1:18pm UTC](https://forum.radzen.com/t/user-information-on-entities/1016/11 "2019-03-22T13:18:29Z")

</div>

@mumfie, how did you manage to pass in the username?

---

<div class="post-metadata">

**Author:** ![korchev](https://forum.radzen.com/user_avatar/forum.radzen.com/korchev/32/4_2.png) [@korchev](https://forum.radzen.com/u/korchev)\
**Post date:** [March 22, 2019, 2:23pm UTC](https://forum.radzen.com/t/user-information-on-entities/1016/12 "2019-03-22T14:23:12Z")

</div>

We just released Radzen 2.12.1 which has the needed changes. Also we released the [AuditTrail](https://github.com/akorchev/radzen/tree/master/AuditTrail) sample application.

There are two custom files that you need to replicate in your application - [server\Startup.Audit.cs](https://github.com/akorchev/radzen/blob/master/AuditTrail/server/Startup.Audit.cs) and [server\data\AuditTrailDbContext.Audit.cs](https://github.com/akorchev/radzen/blob/master/AuditTrail/server/Data/AuditTrailDbContext.Audit.cs) (you should create a partial class for the DB context that Radzen has generated for your database).

The actual logging is done in the Audit method. You can rename the properties that the sample is using to match your existing DB schema, perform additional logging etc:

```auto
public static void Audit(EntityEntry entityEntry, string userName)
{
     var type = entityEntry.Entity.GetType();
     if (entityEntry.State == EntityState.Added)
     {
          SetProperty(type, entityEntry, "CreatedBy", userName);
          SetProperty(type, entityEntry, "CreatedAt", DateTime.UtcNow);
     }
     else if (entityEntry.State == EntityState.Modified)
     {
          SetProperty(type, entityEntry, "UpdatedBy", userName);
          SetProperty(type, entityEntry, "UpdatedAt", DateTime.UtcNow);
     }
}

```

---

<div class="post-metadata">

**Author:** ![Paul\_Pitchford](https://forum.radzen.com/user_avatar/forum.radzen.com/paul_pitchford/32/687_2.png) [@Paul\_Pitchford](https://forum.radzen.com/u/Paul_Pitchford)\
**Post date:** [March 25, 2019, 8:33pm UTC](https://forum.radzen.com/t/user-information-on-entities/1016/14 "2019-03-25T20:33:27Z")

</div>

Now that's what I call support. Thank you so much @korchev!

---

<div class="post-metadata">

**Author:** ![Paul\_Pitchford](https://forum.radzen.com/user_avatar/forum.radzen.com/paul_pitchford/32/687_2.png) [@Paul\_Pitchford](https://forum.radzen.com/u/Paul_Pitchford)\
**Post date:** [March 28, 2019, 10:06am UTC](https://forum.radzen.com/t/user-information-on-entities/1016/15 "2019-03-28T10:06:04Z")

</div>

I've got time to try and implement this, this morning and I'm running into an issue in the SaveChanges override.

It's failing on this line : `var userName = httpAccessor.HttpContext.User.FindFirst(ClaimTypes.Name).Value;`

 ![auditing](https://forum.radzen.com/uploads/default/original/2X/3/3381fc4de0e970674a39730f4dc80ffdf1ca7153.png)

I can see that it's failing to get the logged in userName. Diving into the identity I can see it looks like the logged in user isn't authenticated(?), I think. I've logged the user out and logged it back in and still can't get it to find the username.

Thanks.

---

<div class="post-metadata">

**Author:** ![korchev](https://forum.radzen.com/user_avatar/forum.radzen.com/korchev/32/4_2.png) [@korchev](https://forum.radzen.com/u/korchev)\
**Post date:** [March 28, 2019, 11:18am UTC](https://forum.radzen.com/t/user-information-on-entities/1016/16 "2019-03-28T11:18:14Z")

</div>

This will happen if the first step is missing - the Startup.Audit.cs file.

---

<div class="post-metadata">

**Author:** ![Paul\_Pitchford](https://forum.radzen.com/user_avatar/forum.radzen.com/paul_pitchford/32/687_2.png) [@Paul\_Pitchford](https://forum.radzen.com/u/Paul_Pitchford)\
**Post date:** [March 28, 2019, 11:41am UTC](https://forum.radzen.com/t/user-information-on-entities/1016/17 "2019-03-28T11:41:30Z")

</div>

Typo alert! 😭

Thanks @korchev

---

<div class="post-metadata">

**Author:** ![Paul\_Pitchford](https://forum.radzen.com/user_avatar/forum.radzen.com/paul_pitchford/32/687_2.png) [@Paul\_Pitchford](https://forum.radzen.com/u/Paul_Pitchford)\
**Post date:** [January 21, 2020, 12:42pm UTC](https://forum.radzen.com/t/user-information-on-entities/1016/18 "2020-01-21T12:42:19Z")

</div>

I have been looking at implementing this today in my Blazor sample application and I seem to be running into a problem because the httpAccessor isn't injected into the DbContext when the project is built. I have implemented security I can see by looking at the DbContext that the httpAccessor doesn't exist.

Would it be an issue to implement this in the Blazor projects too?

Thank you.

---

<div class="post-metadata">

**Author:** ![korchev](https://forum.radzen.com/user_avatar/forum.radzen.com/korchev/32/4_2.png) [@korchev](https://forum.radzen.com/u/korchev)\
**Post date:** [January 21, 2020, 1:12pm UTC](https://forum.radzen.com/t/user-information-on-entities/1016/19 "2020-01-21T13:12:21Z")

</div>

The current user in Blazor is obtained by other means: [https://www.radzen.com/documentation/blazor/get-current-user/](https://www.radzen.com/documentation/blazor/get-current-user/)

---

<div class="post-metadata">

**Author:** ![Paul\_Pitchford](https://forum.radzen.com/user_avatar/forum.radzen.com/paul_pitchford/32/687_2.png) [@Paul\_Pitchford](https://forum.radzen.com/u/Paul_Pitchford)\
**Post date:** [January 21, 2020, 2:15pm UTC](https://forum.radzen.com/t/user-information-on-entities/1016/20 "2020-01-21T14:15:51Z")

</div>

Ok, thanks for the link.

I've added:

```
private readonly SecurityService security;

public PaccorDbContext(DbContextOptions<PaccorDbContext> options, SecurityService security) : this(options)
{
    this.security = security;
}

```

To my `MyContext.Audit.cs` class file. and in the `SaveChanges()` method changed:

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

to:

`var userName = security.User.UserName;`

The application builds and starts but I get this error in chrome just as the pages were about to load:

`InvalidOperationException: No authentication handler is registered for the scheme 'Bearer'. The registered schemes are: Identity.Application, Identity.External, Identity.TwoFactorRememberMe, Identity.TwoFactorUserId. Did you forget to call AddAuthentication().Add[SomeAuthHandler]("Bearer",...)?`

---

<div class="post-metadata">

**Author:** ![korchev](https://forum.radzen.com/user_avatar/forum.radzen.com/korchev/32/4_2.png) [@korchev](https://forum.radzen.com/u/korchev)\
**Post date:** [January 21, 2020, 2:41pm UTC](https://forum.radzen.com/t/user-information-on-entities/1016/21 "2020-01-21T14:41:32Z")

</div>

In Blazor you don't need the Startup.Audit.cs file.

[Next page](https://forum.radzen.com/t/user-information-on-entities/1016.md?page=2)
