我正在尝试从远程SharePoint站点(不同的SP Web App)中的列表中读取值。使用Claims Auth设置Web应用程序,并使用SPN配置客户端Web应用程序SP Managed帐户。我相信Kerberos和声明设置正确,但我无法访问远程服务器,请求导致异常:“远程服务器返回错误:(401)未经授权。”
异常发生在行ctx.ExecuteQuery();
中,但它不会捕获if (scope.HasException)
中的异常,而是由调用代码捕获异常(在using {}块之外)。
当我使用Wireshark查看远程服务器上的流量时,它看起来似乎甚至没有到达服务器;它几乎就像401在Kerberos票证交换索赔之前发生的那样。
这是我的代码:
using (ClientContext ctx = new ClientContext(contextUrl))
{
CredentialCache cc = new CredentialCache();
cc.Add(new Uri(contextUrl), "Kerberos", CredentialCache.DefaultNetworkCredentials);
ctx.Credentials = cc;
ctx.AuthenticationMode = ClientAuthenticationMode.Default;
ExceptionHandlingScope scope = new ExceptionHandlingScope(ctx);
Web ctxWeb = ctx.Web;
List ctxList;
Microsoft.SharePoint.Client.ListItemCollection listItems;
using (scope.StartScope())
{
using (scope.StartTry())
{
ctxList = ctxWeb.Lists.GetByTitle("Reusable Content");
CamlQuery qry = new CamlQuery();
qry.ViewXml = string.Format(ViewQueryByField, "Title", "Text", SharedContentTitle);
listItems = ctxList.GetItems(qry);
ctx.Load(listItems, items => items.Include(
item => item["Title"],
item => item["ReusableHtml"],
item => item["ReusableText"]));
}
using (scope.StartCatch()) { }
using (scope.StartFinally()) { }
}
ctx.ExecuteQuery();
if (scope.HasException)
{
result = string.Format("Error retrieving content<!-- Error Message: {0} | {1} -->", scope.ErrorMessage, contextUrl);
}
if (listItems.Count == 1)
{
Microsoft.SharePoint.Client.ListItem contentItem = listItems[0];
if (SelectedType == SharedContentType.Html)
{
result = contentItem["ReusableHtml"].ToString();
}
else if (SelectedType == SharedContentType.Text)
{
result = contentItem["ReusableText"].ToString();
}
}
}
我意识到在声明中不应该使用CredentialCache的部分,但我能找到的每个例子都是在控制台应用程序中运行,或者在某种客户端应用程序中运行;此代码在常规ASP.NET UserControl的代码隐藏中运行。
编辑:我应该提一下,当远程网址是与调用代码(在网站集中的同一网络应用程序)上的根网站集时,上面的代码甚至不起作用在/ sites /)下 - 换句话说,即使主机名与调用代码相同。
非常感谢任何有关下一步尝试的建议!
麦克
答案 0 :(得分:0)
您有没有使用标准OM的原因?
您已经说过这是在Web部件中运行,这意味着它位于应用程序池帐户的上下文中。除非您通过切换用户来提升权限,否则它将无法正确进行身份验证。也许试试吧。但是当你有权访问API时,我不会使用客户端OM。