Scope Variables

ANB 181 Reputation points
2021-12-08T21:43:01.367+00:00

I am trying to understand why my variable is not available for the methods.

a) I create a var at class level (so I can have access to that from any method)
b) At the constructor, I will add to this var the logged in user Id
c) Trying to get _userId variable value inside the method but I get "could not be found"

public class MyController : ControllerBase
{
private readonly string _userId;

    public MyController ()
    {
        _userId = User.Identity.GetUserId();
    }

    [HttpGet]
    public string GetUserId()
    {
        return _userId;
    }
}

Where exact I'm making the scope mistake ?

Thx guys !

Developer technologies | ASP.NET | ASP.NET Core
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    2021-12-09T03:16:15.88+00:00

    Hi @ANB ,

    To access HttpContext or User in constructor, we should use IHttpContextAccessor, so, try to modify your code as below:

    public class MyController : ControllerBase  
    {  
             private readonly string _userId;  
             private readonly string _username;  
             //required using Microsoft.AspNetCore.Http;  
             //required using System.Security.Claims;  
    
             private readonly IHttpContextAccessor  _httpContextAccessor;     
             public MyController (IHttpContextAccessor httpContextAccessor)  
             {  
                 _httpContextAccessor = httpContextAccessor;  
                 _userId = httpContextAccessor.HttpContext.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;  
                 _username = httpContextAccessor.HttpContext.User?Identity?.Name;  
             }  
             [HttpGet]  
             public string GetUserId()  
             {  
                 return _userId;  
             }  
         }  
    

    The result is below:

    156020-image2.gif

    Note: If you meet the IHttpContextAccessor not register error, you can register IHttpContextAccessor in configure method:

     Services.AddHttpContextAccessor();  
    

    Reference: Access HttpContext in ASP.NET Core


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion

    0 comments No comments

  2. Bruce (SqlWork.com) 82,061 Reputation points Volunteer Moderator
    2021-12-15T15:59:54.017+00:00

    The issue is that the User variables value is set after the constructor is called, so it’s null in the constructor. Use the above technique to access user in the constructor.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.