前段时间,我使用asp.net Web-api beta构建了API。我在某处读到,当在IIS中托管时,web-api继承了其运行的Web应用程序中的Identity。我的api也在一个网站上运行。使用测试版的二进制文件,当我登录到网站,然后移动到http://localhost:4343/webui/api/values
它会返回给我登录用户的正确值。这里是值控制器的Get
方法。使用新的ASP.NET Web API的主体
public IQueryable<string> Get()
{
var pr = Request.GetUserPrincipal();
var username = Request.GetUserPrincipal().Identity.Name; //Null reference exception after installing vs 2012 here. Identity is null even though I am logged in
var values = GetUserValues(username);
return values.AsQueryable();
}
它曾经与web-api的beta版本正常工作,但与最终版本的vs2012它抛出空引用异常。我已将该网站转换为使用.net 4.5。任何想法发生了什么?
让我吃惊的是VS 2012在nuget中增加了'System.Net.Http.Formatting'和一些其他程序集。我也认为他们会被GACed,但可能他们不是。 'Thread.CurrentPrincipal'是我采用的路线,但我认为访问'User'属性是更清晰的方法。 –