The request failed with HTTP status 401: Unauthorized问题怎么解决

hansier 2011-10-26 04:21:47
我之前写了一个关于两个sharepoint list (功能是:list1 取 list2 中 Items ) webpart, 调试时正常运行(此处的运行时通过手动点击button 来实现list1 取 list2 中items的功能)。 现在我需要把这同步的功能通过Timer Job 来实现,即通过timer job 让系统自动运行, 但是 我在调试中有几个个问题:
问题1: 调试中出现的为问题:“ The request failed with HTTP status 401: Unauthorized。” 相同的代码,怎么会在之前的button下调试时可以成功,而放到Timer Job 里面却出现了权限的问题呢?

对于这个问题,因为我是让list2 中的数据从list1 去过来的,而这两个list 在不同服务器上,这个的实现上我采用了调用 list webservice 实现了, 在权限问题上我想过如果用 SPSecurity.RunWithElevatedPrivileges(delegate() ) 这个方法,那要怎么去用呢 ?

一下是我在 Timer Job 中 Job类 中 Execute () 方法中片段代码:
public override void Execute(Guid contentDbId)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{

SPWebApplication webApplication = this.Parent as SPWebApplication;

//SPContentDatabase contentDb = webApplication.ContentDatabases[contentDbId];
//SPList myDestinationList = contentDb.Sites[0].RootWeb.Lists["List1"];
//SPWeb myDestinationWeb = contentDb.Sites[0].OpenWeb();


nsplist.Lists List1 = new nsplist.Lists();
List1.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

List1.Url = "http://gdcckm.chn.test.com/teams/nsp/_vti_bin/Lists.asmx";
System.Xml.XmlDataDocument xmlDoc = new System.Xml.XmlDataDocument();
string listName = "{0F57EBB4-B86E-4156-BA15-409DA6769422}";
string rowLimit = "10000";

System.Xml.XmlNode nodeListItems;
System.Xml.XmlElement query = xmlDoc.CreateElement("Query");
System.Xml.XmlElement viewFields = xmlDoc.CreateElement("ViewFields");
System.Xml.XmlElement queryOptions = xmlDoc.CreateElement("QueryOptions");
nodeListItems = List1.GetListItems(listName, null, query, null, rowLimit, null);
xmlDoc.LoadXml(nodeListItems.OuterXml);
XmlNodeList nlist = xmlDoc.GetElementsByTagName("z:row");

SPSite myDestinationSite = new SPSite("http://hswsrdm0");
SPWeb myDestinationWeb = myDestinationSite.OpenWeb();
SPList myDestinationList = myDestinationWeb.Lists["List2"];
..........
............
});
} 可是在这个地方就出现了 权限那个问题,


请大侠们抽出点宝贵时间,帮忙看看。
...全文
4653 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
forum2621 2011-11-14
  • 打赏
  • 举报
回复
补充下,读数据,我建议自己写个web service,掉自己的webservice很方便的,系统的不好用。
forum2621 2011-11-14
  • 打赏
  • 举报
回复
不在同一个服务器上,用webserver调用应该没什么问题吧。
401 错误 有可能是 没加IIS_USer的权限,添加下应该就没问题了。
yanyinandd 2011-11-08
  • 打赏
  • 举报
回复
我觉得这样,第一步,你先单独调试,看具体哪行出的问题。

如果是timer job:这个timer job里面调试,使用system.Environment.CurrentUser(好像是这么写)看看当前user是什么,然后把这个User在当前list1以及list2上添加full control权限。

如果是web service: 这个就需要你设置一下List的Credentials了,windows认证像3楼说的那样,如果是form的就要复杂一些了,现在我这里没有代码。

我在思考你的实现方式,timer job里使用web service的要求。。。跪求高手
  • 打赏
  • 举报
回复
1. 401要看具体是401.?

2.Timer Service自身就是Load balance的,里面的逻辑不用我们来关心。

3.可以enable anonymous 试试。

4.在SharePoint 安装目录中针对web service有一个web.config,可以进行一些修改试试。
hansier 2011-11-01
  • 打赏
  • 举报
回复
有没有高手帮忙看看,希望给点仔细的回答,先谢谢了,真的很急,就这个问题折磨我好久了,,,

1. 401 错误权限这个问题要怎么解决呢?

2. 那两个list 不在同一个服务器上,在Execute () 中要怎么写呢,我的上面的写法不知道对不对,希望高手抽点时间看下我的那代码, 因为在调试的时候只是出现了 401 那个错误,
Justin-Liu 2011-10-27
  • 打赏
  • 举报
回复
401 没有权限
Michael193 2011-10-27
  • 打赏
  • 举报
回复
List.Credentials需要赋值;
List.Credentials = new NetworkCredential(UserName, Password, Domain);

尽量给个比较高的权限;
hansier 2011-10-26
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wang4237 的回复:]

你这个Timer Job是运行在哪个Service上的?
如果是Timer Service。那么将Timer Service的运行用户在site对应的WebApp中分配Full Control的Policy。
[/Quote]
hi,能麻烦写出来让我学习下吗,我不是很清楚要怎么写你所说的policy, 在此先谢谢了啊,,
a-zhwang 2011-10-26
  • 打赏
  • 举报
回复
你这个Timer Job是运行在哪个Service上的?
如果是Timer Service。那么将Timer Service的运行用户在site对应的WebApp中分配Full Control的Policy。

3,244

社区成员

发帖
与我相关
我的任务
社区描述
企业开发 SharePoint
社区管理员
  • SharePoint社区
  • 霖雨 - LinyuLoveTJ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧