The following code works in a regular controller:
[HttpPost]
public ActionResult Custom()
{
string name = User.Identity.GetUserName();
string id = User.Identity.GetUserId();
return Content(string.Format("Name:{0} </br> ID: {1}",name, id));
}
In the Web Api 2 controller, id names and strings are empty:
[HttpPost]
public IHttpActionResult Test()
{
string name = User.Identity.GetUserName();
string id = User.Identity.GetUserId();
return Ok();
}
Can someone tell me why it GetUserId()works in a regular controller, but not in Api? In both cases, I logged in and GlobalConfiguration.Configure(WebApiConfig.Register);added to Application_Start () in Global.asax.cs.
And I have another problem. If I decorate my api controller with an attribute [Authorize], I cannot even access my api. The postman will direct me to the login page when I am already logged in.
[[Authorize]]
public class TestController : ApiController
{
....
source
share