具有 OWIN 自托管和 Windows 身份验证的 Web Api

WebApi with OWIN SelfHost and Windows Authentication

我有一个控制台应用程序服务器,它使用 OWIN 自托管来托管 WebApi 控制器,并在名为 "ServiceTest1" 的自定义帐户下运行。

在同一台机器上,我有另一个控制台应用程序 CLIENT 在帐户 "ServiceTest2" 下运行,我想在 SERVER 中捕获 "ServiceTest2" 调用了控制器操作。然而:

我需要什么才能让这个 WebApi OWIN 自托管来获取调用者的 Windows 身份?

您的问题有点不清楚您是如何实施 Windows 身份验证的。

启用Windows身份验证:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        HttpListener listener = (HttpListener)app.Properties["System.Net.HttpListener"];
        listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;

        // ...
    }
}

在 OWIN 中间件中获取用户:

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

    //...
}

在 Web 中获取用户 API 控制器:

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