Sitecore 7.2 - 项目Web API - 用户身份验证

时间:2014-08-19 12:30:51

标签: c# sitecore sitecore7.2

我想限制Sitecore Item Web API将数据仅发送给经过身份验证的用户&根据文档,我们需要传递用户名和http request header中的密码为X-Scitemwebapi-Username& X-Scitemwebapi-Password

为实现这一目标,我使用了以下代码:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://scapidemo.local/-/item/v1/?sc_itemid={110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}&sc_database=master");

request.Headers["X-Scitemwebapi-Username"] = "admin";
request.Headers["X-Scitemwebapi-Password"] = "b";

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Response.Write(String.Format("Content length is {0}", response.ContentLength));
Response.Write(String.Format("Content type is {0}", response.ContentType));
// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream();

// Pipes the stream to a higher level stream reader with the required encoding format. 
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);

Response.Write("<br /> Response stream received. <br />");
Response.Write(readStream.ReadToEnd());

Sitecore.ItemWebApi.config我已为我的网站添加了如下设置:

itemwebapi.mode="StandardSecurity"
itemwebapi.access="ReadOnly"
itemwebapi.allowanonymousaccess="false"/>

现在,在运行我的应用程序时,我收到此错误:

{"statusCode":401,"error":{"message":"Access to site is not granted."}}  

1 个答案:

答案 0 :(得分:6)

您正在传递没有所属域的用户。 ItemWebAPI没有默认域名,因此每次拨打电话时,您都需要像“域\用户”一样传递您的用户。

所有这一切 - 试试这样:

request.Headers["X-Scitemwebapi-Username"] = @"sitecore\admin";
request.Headers["X-Scitemwebapi-Password"] = "b";