У меня есть этот код для установки 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);
и этот код для извлечения данных из Thread.CurrentPrincipal
:
var principal = Thread.CurrentPrincipal as ClaimsPrincipal;
var id= principal.Claims.Where(c =>
c.Type == "id")?.Select(x => long.Parse(x.Value)).FirstOrDefault();
Когда я запускаю свой код:
//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 имеет значение, но когда я вызываю метод во второй раз, id2 становится пустым ...
Как я могу это исправить?