【问题标题】:Sharepoint Impersonate external web service callSharepoint 模拟外部 Web 服务调用
【发布时间】:2014-09-23 07:49:15
【问题描述】:

我创建了一个在与我的 SharePoint (2013) 应用程序节点不同的机器/IIS 上运行的 Web 服务(出现双跳问题)。我将此 Web 服务公开给我们公司的其他服务。

以下代码 sn-p 将使用专用凭据(即“sp_admin_user”)成功检索 SharePoint 列表。

在我的 Web 服务中,我可以检索调用它的用户的用户名(无密码 ofc),该用户名也按规则存在于 SharePoint 中。

我的问题:我需要如何更改以下代码以方便使用上述用户名进行模拟?

 [WebMethod]
 [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
 public string get_sp_list()
    {
    SPLists.Lists myservice = new SPLists.Lists();
    myservice.Credentials = new System.Net.NetworkCredential( "sp_admin_user", "password123", "domain" );
    myservice.Url = "https://sharepoint.company.com/sites/testground/_vti_bin/Lists.asmx";
    [.. setting of variables ..]
    System.Xml.XmlNode n = myservice.GetListItems(
        sidList,
        sidView, 
        query, 
        viewFields, 
        rowLimit, 
        queryOptions, 
        null
    );
    [.. compose json ..]
    return json;
 }

【问题讨论】:

    标签: c# web-services sharepoint impersonation


    【解决方案1】:

    您可以在没有密码的情况下模拟的唯一用户是 SharePoint IIS 应用程序池用户(通常是网络服务)。要模拟访问 SharePoint 页面的用户,运行 SharePoint 的计算机需要对外部服务具有委派权限。你可以阅读它here。这都是关于安全限制的。

    我建议您拒绝模拟方法,它更容易处理密码、进行匿名 Web 服务调用或发明其他东西。

    你可以这样冒充用户:

    using (var ctx = WindowsIdentity.Impersonate(IntPtr.Zero))
    {
        //external web service call as "COMPUTER_NAME\NETWORK_SERVICE" user
    }
    

    如果您有密码,您还可以获得真实的用户令牌(不是 IntPtr.Zero)。

    【讨论】:

    • 感谢您的回复。我希望有一个简单的 Web 服务调用来切换用户上下文,毕竟我以管理员身份进行身份验证。此外,我提供的大多数 Web 服务 API 都为您提供了此功能。