How to do ASP.NET Web API integration tests with custom authentication and in-memory hosting

时间秒杀一切 提交于 2019-12-06 13:38:42

I have had success following the guidance listed in the "Retrieving and Assigning the Current Principal" section of Chapter 15 in "Designing Evolvable Web APIs with ASP.NET":

In ASP.NET Web API version 2.0, you can solve this problem by using the new HttpRequestContext class. First, the current identity should be retrieved and assigned to the current request object, not to a static property. Secondly, different hosts can use different HttpRequestContext implementations

In short, in your message handler, do this instead of setting the current Thread and HttpContext's principal:

request.GetRequestContext().Principal = principal;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!