In a regular controller the following code works:
[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 a Web Api 2 Controller the name and id strings are empty:
[HttpPost]
public IHttpActionResult Test()
{
string name = User.Identity.GetUserName();
string id = User.Identity.GetUserId();
return Ok();
}
Can anyone tell me why GetUserId()
works in a normal controller but not in an
Api? In both cases i am logged in, and GlobalConfiguration.Configure(WebApiConfig.Register);
is added in Application_Start()
in Global.asax.cs
.
And i have another problem. If i decorate my api controller with [Authorize]
attribute, I can't even access my api. The Postman will direct me to the Login page, when a I am already logged in.
[[Authorize]]
public class TestController : ApiController
{
....