Salman Shaykh
Salman Shaykh

Reputation: 221

Sitecore-Web Api User authentication

I want to send the sitecore data to authenticated user only..How to pass the HttpWebRequest object to DownloadString function of webclient....DownloadString cannot take HttpWebRequest as a parameter.I was following this link Sitecore 7.2 - Item Web API-User Authentication

var client = new WebClient();

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

    request.Headers["X-Scitemwebapi-Username"] = "admin";
    request.Headers["X-Scitemwebapi-Password"] = "b";
 var apiResponse = client.DownloadString(request);
            dynamic jsonResponse = JObject.Parse(apiResponse);

Upvotes: 2

Views: 2994

Answers (3)

Brad Christie
Brad Christie

Reputation: 101614

I happen to like using RestSharp over HttpWebRequest, so thought I'd add another version to the list:

// Build a client pointing to your sitecore instance
IRestClient restClient = new RestClient("http://gyldendal.local/");
// Add credentials to all future requests
restClient.AddDefaultHeader("X-Scitemwebapi-Username", @"sitecore\admin");
restClient.AddDefaultHeader("X-Scitemwebapi-Password", "b");

// Build a request to the item api
IRestRequest restRequest = new RestRequest("/-/item/v1/", Method.GET);
// specify API parameters
restRequest.AddParameter("sc_database", "master");
restRequest.AddParameter("sc_itemid", "{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}");

#if DEBUG
Console.WriteLine( restClient.BuildUri(restRequest) ); // http://gyldendal.local/-/item/v1/?sc_database=master&sc_itemid={110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}
#endif

// Execute request
IRestResponse restResponse = restClient.Execute(restRequest);

// Confirm successful response
if (restRequest.StatusCode == HttpStatusCode.OK)
{
    /* Work with restResponse.Content */
#if DEBUG
    Console.WriteLine(restResponse.Content); // {"statusCode":200,"result":{....}}
#endif
}

Also, just so it's clear, confirm the itemwebapi has been enabled on your specific <site>. For a default install, you can use the following patch file to enable it:

<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <sitecore>
    <sites>
      <site name="website">
        <!-- Options: Off|StandardSecurity|AdvancedSecurity -->
        <patch:attribute name="itemwebapi.mode">StandardSecurity</patch:attribute>
        <!-- Options: ReadOnly|ReadWrite -->
        <patch:attribute name="itemwebapi.access">ReadOnly</patch:attribute>
        <!-- Options: True|False -->
        <patch:attribute name="itemwebapi.allowanonymousaccess">False</patch:attribute>
      </site>
    </sites>
  </sitecore>
</configuration>

Upvotes: 1

RobertoBr
RobertoBr

Reputation: 1801

You can get the response from the request object directly.

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("...");

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

    //(...)

    using (var response = (HttpWebResponse)request.GetResponse())
    {
       var stream = response.GetResponseStream();
       var reader = new StreamReader(stream);

       string apiResponse = reader.ReadToEnd();
    }

Also, you need to inform the domain in the user name. Like sitecore\admin

Cheers

Upvotes: 2

Vasiliy Fomichev
Vasiliy Fomichev

Reputation: 171

...and here is a way to do it using the Web API framework; just because i am a fan of async.

using (var client = new HttpClient())
            {
                var request = new HttpRequestMessage
                {
                    RequestUri = new Uri("..."),
                    Method = HttpMethod.Get
                };

                request.Headers.Add("X-Scitemwebapi-Username", "sitecore\admin");
                request.Headers.Add("X-Scitemwebapi-Password", "b");

                var response = await client.SendAsync(request);
                if (response.IsSuccessStatusCode)
                {
                    var responseString= await response.Content.ReadAsStringAsync();
                    ...
                }
            }

Upvotes: 3

Related Questions