前段時間,我使用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'屬性是更清晰的方法。 –