HttpContext.Current.User空使用Web API v2的唯一项目 [英] HttpContext.Current.User null with Web API v2 only project

查看:432
本文介绍了HttpContext.Current.User空使用Web API v2的唯一项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是使用IWA(集成Windows身份验证)与.NET的Web API。我想保持该项目作为只是一个网络API项目,没有MVC。

My goal is to use IWA (Integrated Windows Authentication) with .NET Web API. I want to keep the project as just an Web API project, without MVC.

我创建了两个项目在Visual Studio 2013 premium:

I created two projects in Visual Studio 2013 Premium:

  1. 第一,我选择与身份验证Windows身份验证的Web API模板ASP.NET Web应用程序。这也包括ASP.NET MVC。
  2. 第二个是一个ASP.NET Web应用程序的空模板的Web API的核心引用。

第一个工作,我能打电话的时候,让我的帐户AD / Windows标识:

The first one worked, I was able to get my account AD/Windows identity when calling:

 HttpContext.Current.User

第二个我不能让我的AD / Windows的身份,我尝试了所有的以下内容:

The second I couldn't get my AD/Windows identity, I tried all of the following:

 - HttpContext.Current.User
 - User
 - RequestContext.Principal

什么我需要从Web API(第二个项目)来调用来填充用户?

What do I need to call from the Web API (second project) to populate the user?

推荐答案

网络API V2采用OWIN(武士刀),使IWA与IIS,你需要导入Microsoft.Owin.Host.SystemWeb library.This使用IIS与ASP.NET管道,其中填充用户。

Web API v2 uses OWIN (Katana), to enable IWA with IIS you need to import the Microsoft.Owin.Host.SystemWeb library.This uses IIS with the ASP.NET pipeline, which populates User.

说明设置 OWIN与网络API ,注意事项:

  1. 在这篇文章是从2013年开始,从那时起Microsoft.Owin.Host.SystemWeb不再pre-释放,例如你并不需要 - pre,从的NuGet安装
  2. 删除Startup.cs的配置方法,里面的内容。

这篇关于HttpContext.Current.User空使用Web API v2的唯一项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆