用的WebAPI OWIN SelfHost和Windows身份验证 [英] WebApi with OWIN SelfHost and Windows Authentication

查看:624
本文介绍了用的WebAPI OWIN SelfHost和Windows身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须承载在一个名为ServiceTest1自定义帐户使用OWIN自托管的WebAPI控制器,​​并运行一个控制台应用程序服务器

I have a console application SERVER that hosts WebApi controllers using OWIN self-hosting, and runs under a custom account named "ServiceTest1".

在同一台机器我有帐户ServiceTest2下运行另一个控制台应用程序客户端,我想在服务器ServiceTest2援引一个控制器动作捕捉。但是:

In the same machine I have another console application CLIENT that runs under the account "ServiceTest2", and I want to capture in SERVER that "ServiceTest2" invoked a controller action. However:


  • WindowsIdentity.GetCurrent()始终是ServiceTest1。

  • Thread.CurrentPrincipal中是一个未经 GenericIdentity

  • RequestContext.Principal 为null。

  • 用户为null。

  • WindowsIdentity.GetCurrent() is always "ServiceTest1".
  • Thread.CurrentPrincipal is an unauthenticated GenericIdentity.
  • RequestContext.Principal is null.
  • User is null.

我需要什么,使这个的WebAPI OWIN自托管抢主叫方的Windows标识?

What do I need to make this WebApi OWIN self-hosted to grab the Windows identity of the caller?

推荐答案

您的问题有点不清楚究竟上你是如何实现的Windows身份验证。

Your question is a little unclear on exactly how you've implemented the Windows authentication.

启用Windows身份验证:

Enable Windows authentication:

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

        // ...
    }
}

获取用户在OWIN中间件:

Get the user in an OWIN middleware:

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

    //...
}

获取用户在Web API控制器:

Get the user in a Web API Controller:

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

这篇关于用的WebAPI OWIN SelfHost和Windows身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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