While trying to implement the answer from here > How to get the current logged in user Id in ASP.NET Core and the user redirected me to here > https://github.com/dotnet/aspnetcore/issues/18348
var UserId = User.FindFirstValue(ClaimTypes.Name);
^ This is not working, and prints the following error 'User' does not contain a definition for 'FindFirstValue'
Edit: Adding controller snippet
The full snippet of my controller...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using ProjectName.Processing;
using ProjectName.Repository;
using Newtonsoft.Json;
namespace ProjectName.Controllers
{
[ApiController]
[Route("api/[controller]/[action]")]
public class ClassNameController : ControllerBase
{
private readonly ILogger<ClassNameController> _logger;
private OtherClassNameProcessing proc = new OtherClassNameProcessing();
public ClassNameController(ILogger<ClassNameController> logger)
{
_logger = logger;
}
[HttpGet]
[ProducesResponseType(typeof(List<2ndOtherClassNameRepository>), StatusCodes.Status200OK)]
public IActionResult GetUserName()
{
var data = proc.GetUserName();
return Ok(data.Result);
}
}
}
The full snippet my controller is calling...
using Microsoft.EntityFrameworkCore;
using ProjectName.Data;
using ProjectName.Repo;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Web;
using System.Threading.Tasks;
using System.Security.Claims;
using Microsoft.AspNetCore.Http;
using System.Web.Providers.Entities;
namespace ProjectName.Processing
{
public class OtherClassNameProcessing
{
private readonly ProjName_DevelContext _context = new ProjName_DevelContext();
private async Task<List<2ndOtherClassNameRepository>> GetNameFromRepo()
{
List<2ndOtherClassNameRepository> repoList = new List<2ndOtherClassNameRepository>();
var Temp = await _context.tablename.ToListAsync();
/* Working Test
2ndOtherClassNameRepository repo = new 2ndOtherClassNameRepository();
repo.UserName = "JohnDoe";
repoList.Add(repo);
return repoList;
*/
2ndOtherClassNameRepository repo = new 2ndOtherClassNameRepository();
// repo.UserName = "JohnDoe";
var userId = User.FindFirstValue(ClaimTypes.Name);
repo.UserName = userId;
repoList.Add(repo);
return repoList;
}
internal async Task<List<2ndOtherClassNameRepository>> GetUserName()
{
return await GetNameFromRepo();
}
}
}
Any idea why I'm getting this error?