2

在参考了这篇文章后,我为基本 Http Authentication 编写了一个属性,用于 web api。在属性类中,如果提供的凭据匹配,那么我将 HttpContext.Current.User 设置为

 HttpContext.Current.User = new GenericPrincipal(new ApiIdentity(apiUser), new string[] {});

base.OnActionExecuting(actionContext);

但是,当我在我的 api 控制器中访问 User 时,它没有 apiUser 属性。

为什么会这样,设置身份然后在 api 控制器中访问它的正确方法是什么?

4

2 回答 2

2

不-不幸的是,您必须同时设置两者。

Thread.CurrentPrincipal 和 HttpContext.User

于 2012-12-20T06:00:52.320 回答
1

在您的 ASP.NET Web API 应用程序中,您不应该依赖当前的HttpContext. 至于您的问题的答案,将主体设置Thread.CurrentPrincipal为您想要的:

Thread.CurrentPrincipal = new GenericPrincipal(new ApiIdentity(apiUser), new string[] {});
于 2012-12-19T19:16:24.767 回答