locked
Get User Id RRS feed

  • Question

  • User-1673823827 posted

    Dear All,

    I am learning Asp.Net MVC and Web Api and I have an MVC project with a Web Api included in the same project. I am trying to access the UserId of the Logged in user in the Web Api code but it keeps returning null.

    // I am using this line of code
    
    var userId = User.Identity.GetUserId();

    Please assist me because I need to post a data which includes the Id of the logged in user.

    Tuesday, June 20, 2017 12:02 PM

Answers

  • User1967761114 posted

    Hi Sahihkoda,

    According to your description, I’m unsure which error was happened with you. 

    I suggest you could try to check whether the user had been logged before get user id, such like the following code:

    if (User.Identity.IsAuthenticated)
    {
        var userId = User.Identity.GetUserId();
    }

    Could you provide more details description about this issue? such as the code of how to authenticate between client and Web API server.

    I recommend you could read the article by the following link to understand how to authenticate in Web API.

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/individual-accounts-in-web-api

     

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 21, 2017 2:28 AM

All replies

  • User-359936451 posted

    Has the user authenticated prior to this request? That is to say, has the user successfully logged in?

    Tuesday, June 20, 2017 12:29 PM
  • User-1673823827 posted

    Yes please. The user is logged in. I must add that I am using that same line of code in an MVC controller and that one seems to be working fine. But the one in the Web Api controller keeps returning null

    Tuesday, June 20, 2017 12:34 PM
  • User1967761114 posted

    Hi Sahihkoda,

    According to your description, I’m unsure which error was happened with you. 

    I suggest you could try to check whether the user had been logged before get user id, such like the following code:

    if (User.Identity.IsAuthenticated)
    {
        var userId = User.Identity.GetUserId();
    }

    Could you provide more details description about this issue? such as the code of how to authenticate between client and Web API server.

    I recommend you could read the article by the following link to understand how to authenticate in Web API.

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/individual-accounts-in-web-api

     

    If you have any other questions, please feel free to contact me any time.

    Best Regards

    Even

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 21, 2017 2:28 AM
  • User1120430333 posted

    You have a service running on a machine that is not the client's machine. I don't think you are going to get the user-id of the client trying to consume the Web API.

    User.Identity.GetUserId();

    The above statement needs to be executed on the client machine by the client application, and the userid is passed into a Web API method on the method's signature.

    Wednesday, June 21, 2017 11:36 AM
  • User-1673823827 posted

    Everything seems fine now. To be honest I have no idea how. I decorated the Action Method with [Authorize] and it worked. So I took it off and rebuild the projection and it seems to be working if the user is logged in. Thanks soo much for the help. The community is amazing!!!

    Wednesday, June 21, 2017 2:02 PM