问题描述
我正在为一个在其域上有服务器的客户工作.我的工作站在我自己的域中.我通常使用我的客户端域用户名和密码 VPN 进入客户端网络.以下查询的问题是我在执行查询时收到 401 Unauthorized access 错误,大概是因为发送了错误的凭据.
I am doing work for a client that has a server on his domain. My workstation is on my own domain. I typically VPN into the client network using my client domain username and password. The problem with the below query is that I get a 401 Unauthorized access error when I execute the query, presumably because the wrong credentials are being sent.
客户建议提供我的客户域凭据,可能包含在 #if DEBUG 中,以便代码在开发环境中正常工作.
The client has suggested supplying my client domain credentials, perhaps wrapped in a #if DEBUG so that the code will work properly in development environments.
我如何提供凭据?以下代码将出现在 ASP MVC 2 项目中.
How do I supply credentials? The following code will be present in an ASP MVC 2 project.
ClientContext clientContext = new ClientContext(URL);
List list = clientContext.Web.Lists.GetByTitle("My Documents");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml = XML;
ListItemCollection listItems = list.GetItems(camlQuery);
clientContext.Load(
listItems,
items => items.Include(item => item["FileRef"]));
clientContext.ExecuteQuery();
我更喜欢以编程方式执行此操作
I prefer to do this programatically
推荐答案
这就是我需要做的:
System.Net.NetworkCredential cred = new System.Net.NetworkCredential("myname", "mypassword");
ClientContext clientContext = new ClientContext(URL);
clientContext.Credentials = cred;
我仍然没有成功,但也许这里还有其他一些问题.
I am still not getting success, but perhaps there are some other issues here.
这篇关于如何提供凭据以在属于另一个域的服务器上执行查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!