【发布时间】: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