0

I am developing a net 5 api and am not able to do something so obvious, retrieve current user id. I have read all the possible articles and tried various approaches, but it is not working. I am aware that this question might seem banal to someone, but I am stuck. What confuses me is the fact that I can retrieve user mail without a problem, but with userid I get the aforementioned error in postman. Here is my code, I am supposed to retrieve transactions that have the same userid, I skipped some parts for brevity, thanks in advance to everyone willing to help:

 public class StockTransaction : BaseEntity
{ 
    public string UserId { get; set; }    // this is users's id  
    public string Email { get; set; }     // this is user's mail

}

Services:

 private async Task<decimal> TotalNetProfit5(string userId)
    { 
        decimal basket = 0;

        foreach (var item in _context.Stocks.ToList())
        {
            var totalNetProfit = (_context.StockTransactions
            .Where(t => t.UserId == userId && t.StockId == item.Id && t.Purchase == false)
            .Sum(t => t.Price * t.Quantity)) - 
            (_context.StockTransactions
            .Where(t => t.UserId == userId && t.StockId == item.Id && t.Purchase == true && 
 t.Resolved > 0)
            .Sum(t => t.Resolved * t.Price));

            basket += totalNetProfit;
        }

        return await Task.FromResult(basket);
    }

       public async Task<TaxLiabilityVM> ReturnTaxLiability7(string userId)
    {
        decimal f = 100;
        decimal? basket4 = await TotalNetProfit5(userId);
        decimal? basket5 = (12 / f) * basket4;
        decimal? basket6 = (18 / f) * basket5;
        decimal? basket7 = basket5 + basket6;
        decimal? basket8 = basket4 - basket7;

        var taxLiability = new TaxLiabilityVM
        {
            GrossProfit = basket4,
            CapitalGainsTax = basket5,
            Surtax = basket6,
            TotalTaxLiaility = basket7,
            NetProfit = basket8
        };

        return await Task.FromResult(taxLiability);
    }

Controller:

[HttpGet]
public async Task<ActionResult<TaxLiabilityVM>> CheckTotalProfit11()
    
    var user = await _userManager.FindByEmailFromClaimsPrinciple(User);

    var userId = user.Id;
      
    var taxLiability = await _transactionService.ReturnTaxLiability7(userId);

    return Ok(taxLiability);

}

Of course, I have also tried the most obvious approach with:

      var userId =  User.FindFirstValue(ClaimTypes.NameIdentifier); 

As I said before, everything works if I replace userid with email...

Petar Sardelic
  • 69
  • 1
  • 2
  • 7

0 Answers0