3

我有一个控制台应用程序服务器,它使用OWIN自托管承载WebApi控制器,并在名为“ServiceTest1”的自定义帐户下运行。带OWIN SelfHost和Windows身份验证的WebApi

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

  • WindowsIdentity.GetCurrent()始终是“ServiceTest1”。
  • Thread.CurrentPrincipal是未经认证的GenericIdentity
  • RequestContext.Principal为空。
  • User为空。

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

回答

12

你的问题有点不清楚你是如何实现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; 
+0

那是什么'ENV '参数?我有正常的从ApiController继承的WebApi控制器。 – vtortola

+0

啊,我已经读过你的问题,因为想要一个OWIN方法来抓取身份。我也使用Web API版本编辑了我的回复。 – Zephyr

+0

'RequestContext.Principal'为null,任何想法为什么? – vtortola