Retrieving the current user ID in a class library outside of an ASP.NET Webforms application using forms authentication

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; } } 
0
source share
2 answers

When authenticating the user, the user can use FormsAuthenticationTicket to transfer all cookie information and additional userData (string) to it, here is an example , then return it:

  FormsIdentity id = (FormsIdentity)User.Identity; FormsAuthenticationTicket ticket = id.Ticket; //those are label ids, that why they have Text property cookiePath.Text = ticket.CookiePath; expireDate.Text = ticket.Expiration.ToString(); expired.Text = ticket.Expired.ToString(); isPersistent.Text = ticket.IsPersistent.ToString(); issueDate.Text = ticket.IssueDate.ToString(); name.Text = ticket.Name; userData.Text = ticket.UserData; // string userData you passed is HERE version.Text = ticket.Version.ToString(); 

Additional Information

By the way, you should use HttpContext.Current.User.Identity.Name , look at this and this , maybe this question .

+1
source

You mentioned that you have many classes that have CreatedBy and ModifiedBy properties.

Ask them to implement the interface:

 interface IAuditableEntity { int CreatedBy {get; set;} int ModifiedBy {get; set;} } public class Product : IAuditableEntity { public string ProductName { get; set; } public string SerialNumber { get; set; } public int CreatedBy {get; set;} public int ModifiedBy {get; set;} } public class Vendor : IAuditableEntity { public string VendorName{ get; set; } public string VendorNumber { get; set; } public int CreatedBy {get; set;} public int ModifiedBy {get; set;} } 

Create a central location in your BLL communication web application that sets these properties for you.

 public class BllLink { public static void SaveEntity(IAuditableEntity entity) { entity.ModifiedBy = HttpContext.Current.User.Identity.Name; if(String.IsNullOrEmpty(entity.CreatedBy)) //assume new { entity.CreatedBy = HttpContext.Current.User.Identity.Name; } Bll.SaveEntity(entity); //you might need to use generics } } 

When you see duplicate code, this is a good candidate for simplification. Learning how to use generics and interfaces effectively will be of great importance for this.

+1
source

All Articles