我正在开发一个具有混合身份验证(基于 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;
关于asp.net - "System.Net.HttpListener"在 IAppBuilder 属性中不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44187991/