I have this code to set Thread.CurrentPrincipal :
var claims = new List<System.Security.Claims.Claim>();
//set Active Customer Id
claims.Add(new Claim("id",100, ClaimValueTypes.Integer32));
var claimsIdentity = new ClaimsIdentity(claims);
//Set the principal to a new generic principal.
Thread.CurrentPrincipal = new ClaimsPrincipal(claimsIdentity);
and this code to retrieve data from Thread.CurrentPrincipal
:
var principal = Thread.CurrentPrincipal as ClaimsPrincipal;
var id= principal.Claims.Where(c =>
c.Type == "id")?.Select(x => long.Parse(x.Value)).FirstOrDefault();
When I run my code :
//retrieve id from principal
var id1 = identityService.GetBaseEntityProps();
//add product
await Product.Add(new Product() { Namt = "test"});
//save change
await unitOfWork.SaveChangesAsync();
//call previous method for retrieve id
//but its null!
var id2 = identityService.GetBaseEntityProps();
id1 has value, but when I call method for second time id2 is null...
How can I fix this?