问题描述
我要限制的项目Sitecore的网络API将数据发送到只与放大器身份验证的用户;根据文档,我们需要通过用户名和放大器;密码 HTTP请求头
为 X-Scitemwebapi-用户名
&安培; X-Scitemwebapi-密码
要做到这一点,我用下面的代码:
HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create(HTTP://scapidemo.local/-/item/v1/ sc_itemid = {110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}&安培; sc_database高手);
request.Headers [X-Scitemwebapi-用户名] =admin的;
request.Headers [X-Scitemwebapi-密码] =B;
HttpWebResponse响应=(HttpWebResponse)request.GetResponse();
的Response.Write(的String.Format(内容长度{0},response.ContentLength));
的Response.Write(的String.Format(内容类型{0},response.ContentType));
//获取与响应相关的流。
流receiveStream = response.GetResponseStream();
//管道流与所需的编码格式更高的水平流读取器。
StreamReader的readStream =新的StreamReader(receiveStream,Encoding.UTF8);
的Response.Write(< BR />响应流中接收到的LT; BR />中);
的Response.Write(readStream.ReadToEnd());
在 Sitecore.ItemWebApi.config
我已经添加如下设置为我的网站:
itemwebapi.mode =StandardSecurity
itemwebapi.access =只读
itemwebapi.allowanonymousaccess =FALSE/>
现在一边跑我的应用我得到这个错误:
{的StatusCode:401,错误:{消息:访问到网站的不批。}}
正在传递用户无需域名属于。该ItemWebAPI不必每次让你做你需要通过你的用户喜欢这种domain\user呼叫时默认域等等。
所有这一切说 - 尝试这样的:
request.Headers [X-Scitemwebapi-用户名] = @sitecore\admin ;
request.Headers [X-Scitemwebapi-密码] =B;
I want to restrict the Sitecore Item Web API to send data to authenticated user only & as per the documentation,we need to pass the user name & password in http request header
as X-Scitemwebapi-Username
& X-Scitemwebapi-Password
To achieve this,I used below code:
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());
In Sitecore.ItemWebApi.config
I've added setting for my website as below:
itemwebapi.mode="StandardSecurity"
itemwebapi.access="ReadOnly"
itemwebapi.allowanonymousaccess="false"/>
Now while running my app I'm getting this error:
{"statusCode":401,"error":{"message":"Access to site is not granted."}}
You are passing the user without the domain it belongs to. The ItemWebAPI does not have a default domain so every time you make you make a call you need to pass your user like this "domain\user".
All that said - try it like this:
request.Headers["X-Scitemwebapi-Username"] = @"sitecore\admin";
request.Headers["X-Scitemwebapi-Password"] = "b";
这篇关于Sitecore的7.2 - 项Web API,用户身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!