[英]Injected IPrincipal is anonymous but Web API Controller User object is authenticated
[英]No IPrincipal user information in controller
我有一個基本的Web API服務,其中的一個控制器繼承自ApiController。 我已經閱讀到我應該能夠利用ApiController.User來獲取當前經過身份驗證的用戶IPrincipal用戶對象。 但是,當我在構造函數中嘗試以下操作時,用戶為空,沒有主體詳細信息。
[InjectionConstructor]
public CloudConfigValueController(IDataModelConverter dataModelConverter, IBusinessModelConverter businessModelConverter, ILogger logger)
{
var u = User;
_businessModelConverter = businessModelConverter;
_dataModelConverter = dataModelConverter;
_logger = logger;
}
我的web.config
<system.web>
<authentication mode="Windows" />
<identity impersonate="true"/>
那么,為什么我的控制器中沒有身份信息?
注意:出於興趣,我還嘗試使用屬性來驗證對API Controller的輸入,並且發現如果深入研究許多對象層,則可以找到Windows用戶名的文本表示形式,但是找不到IPrincipal。 identity.Name下面包含用戶名
public override void OnAuthorization(HttpActionContext actionContext)
{
var wrapper = actionContext.ControllerContext.Request.Properties["MS_HttpContext"] as HttpContextWrapper;
if (wrapper == null)
{
Log.InfoFormat("Could not get http context.");
throw new AuthenticationException("Could not get authorization context");
}
var identity = wrapper.Request.LogonUserIdentity;
if (identity == null)
{
Log.InfoFormat("Could not disover identity");
throw new AuthenticationException("Could not get users identity");
}
}
僅供參考,我實際上發現了問題。 如果轉到Web API項目的屬性面板(而不是右鍵單擊->屬性),即轉到csproj文件的屬性面板,則那里有啟用Windows身份驗證和禁用匿名身份驗證的選項。 這工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.