【问题标题】:Pass windows credentials from Web Api to SharePoint Api将 Windows 凭据从 Web Api 传递到 SharePoint Api
【发布时间】:2019-09-09 22:25:17
【问题描述】:

我有一个 Web api,它被一个 Angular Web 应用程序通过 Windows 身份验证调用。我需要使用通过 Windows 身份验证传递的凭据进入共享点 api。

以下是我目前拥有的代码示例。 (请注意,... 表示我省略了一些代码的地方,还有一些用法,我不认为为此需要在 Sharepoint 上所做的代码。)

using Microsoft.SharePoint.Client.Utilities;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web.Http;
using System.Security.Claims;
using Microsoft.SharePoint.Client;
using System.Web;
using SP = Microsoft.SharePoint.Client;
using System.Security.Principal;
using System.Net;

...

 public static List<SPDocument> GetSPDocumentsById(int itemId)
 {  
    using (ClientContext context = new ClientContext("https://...")
    {                     
        //This I believe is giving me the IIS user Identity, and not the Windows
        //credentials that the user is logging into the API with. 
        context.Credentials = CredentialCache.DefaultNetworkCredentials;

        ...

    }
 }


我的问题与此问题中的问题非常相似:IIS Application Pool Identity to use Logged On User Windows Identity,但解决此问题的步骤并不能解决我的问题。如果我检查 HttpContext.Current.User.Identity.Name 的值,在控制器内

我得到了我期望的用户名,但如果我检查WindowsIdentity.GetCurrent().Name;,我得到的是 IIS 用户帐户。如果WindowsIdentity.GetCurrent().Name; 调用返回了登录的用户 Windows 帐户,这将解决我的问题。

【问题讨论】:

  • 如果您使用的是 Web API,您可以使用 Controller 的 User 属性获取当前的 Windows Authenticated 用户。然后您可以获取 User.Identity 等。
  • @Jon 我在一个独立于我的控制器的服务中拥有它,所以我想尽可能避免从控制器传递数据。
  • 澄清一下,这段代码在 web api 调用的类库中。
  • 您需要将 Windows 凭据传递到类库中(来自 Web API)。假设您的 Web API 使用 Windows 身份验证。
  • 我认为作为 web api 项目一部分的类库应该可以访问用户。我认为我的问题是 defaultNetworkCredentials 返回的不是登录用户。

标签: c# asp.net-web-api sharepoint


【解决方案1】:

原来这是一个 Kerbos 双跃点问题,如本文 (https://blogs.technet.microsoft.com/askds/2008/06/13/understanding-kerberos-double-hop/) 中所述,为了解决此问题,我们只需将 api 移到与它试图命中的其他资源相同的服务器上。

【讨论】:

    猜你喜欢
    • 2014-12-13
    • 2014-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多