Currently, in each of my model classes, I am doing the following in the constructor.
public Product() { CreatedBy = System.Threading.Thread.CurrentPrincipal.Identity.Name; ModifiedBy = System.Threading.Thread.CurrentPrincipal.Identity.Name; }
Thus, when saving / updating records, these values will always be set.
However, I want to switch my application using a username that is out of the above to use a registered user ID.
Using forms authentication, the best way to get more information about the current registered user without using a session? I noticed that you can transfer additional data when creating auth cookie forms as soon as the user logs in, but I did not know how to get it.
Here is the solution I came across. I would be grateful for any comments or suggestions. It seems strange that every time I create one of these model objects in the user interface, I need to pass userId in the constructor:
public abstract class AuditableEntity : BaseEntity { public DateTime CreatedDate { get; set; } public int? CreatedBy { get; set; } public DateTime ModifiedDate { get; set; } public int? ModifiedBy { get; set; } } public class Product : AuditableEntity { public User(int userId) { this.CreatedBy = userId; this.ModifiedBy = userId; } public string ProductName { get; set; } public string SerialNumber { get; set; } }
source share