asp.net - "System.Net.HttpListener"在 IAppBuilder 属性中不可用

标签 asp.net asp.net-web-api active-directory owin windows-authentication

我正在开发一个具有混合身份验证(基于 Owin token 和 Windows 身份验证)的 Web 应用程序。我已经实现了基于 Owin token 的身份验证,并希望为标记为事件目录用户的用户实现 Windows 身份验证。

在 Owin 中间件中,我想获取请求用户的 Windows 用户名。我在 OwinContext.Request.User.Identity 中获取对象。但是,OwinContext.Request.User.Identity.Name 始终为空字符串。

我发现我应该在startup.cs中添加以下几行:

var listener = (HttpListener)app.Properties["System.Net.HttpListener"];
listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;

但是,我遇到了找不到 key 的异常。 Properties 数组中不存在“System.Net.HttpListener”。我已经安装了 Microsoft.Owin.SelfHost、Microsoft.Owin.Host.HttpListener。但是,我仍然收到错误。

非常感谢任何帮助。

谢谢, GB

最佳答案

对我来说,问题是项目是作为共享库启动的,而不是网络应用程序。

解决方案是在 .cspro 文件中 <ProjectGuid> 之后添加一行线。

<ProjectTypeGuids>{349C5851-65DF-11DA-9384-00065B846F21};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

然后您不需要显式添加 HttpListener,只需重新加载项目并按照从属性版本部分开始的说明进行操作即可。 Enabling Windows Authentication in Katana

您可以通过相同的方式访问主体:

在 OWIN 中间件中获取用户:

public async Task Invoke(IDictionary<string, object> env)
{
    OwinContext context = new OwinContext(env);
    WindowsPrincipal user = context.Request.User as WindowsPrincipal;

    //...
}

在 Web API Controller 中获取用户:

// In a web api controller function
WindowsPrincipal user = RequestContext.Principal as WindowsPrincipal;

更新:List of Visual Studio Projects GUIDs

关于asp.net - "System.Net.HttpListener"在 IAppBuilder 属性中不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44187991/

相关文章:

c# - 是否可以迭代Asp.Net Web服务中的循环

c# - 从 session 获取文件时获取 "Cannot access a closed file"错误消息

c# - 外部组件引发的异常

asp.net - Web api Controller 具有两个具有相同参数的 get 方法

angularjs - 通过 id 以及字符串 angularjs webapi 获取

java - Spring security 版本 3.1 - 需要使用未添加域的 userPrincipalName 或 sAMAccountName 来匹配用户(搜索过滤器?)

c# - 如何在桌面上获得与 IIS7 处理交互的许可?

c# - 返回 JSON 错误消息,IActionResult

.net - Powershell GUI - 是否有用于选择 OU 的 Active Directory 对话框?喜欢 FileOpenDialog?

c# - 扩展 GroupPrincipal 和 Members 属性