I started development with Sitefinity 8.1, and I need to access Sitefinity WCF web services (e.g. ~ \ Sitefinity \ Services \ Ecommerce \ Catalog \ ProductService.svc)
I tried to access them, like any other web service, but I got a 401 error. After searching the Internet and the Sitefinity forum, I found a couple of things.
I need to authenticate before using the services [1 & 2]
Claims-based authentication is the default authentication.
The URL used for authentication is /Sitefinity/Services/Security/Users.svc/authenticate [1 and 2]
I also found a fragment provided by Ivan Dimitrov where he encodes an authentication code [3]
Api client is useless to authenticate and resolve a request for web services
This requires STS for authentication and is integrated into my Sitefinity installation [2] "You might be wondering where this STS is. By default, the logic is integrated into your Sitefinity application and can be found in ~ / Sitefinity / SWT." [2]
After I read this information, I adapted the code provided by Ivan Dimitrov [3] and encoded Call in ~ \ Sitefinity \ Services \ Ecommerce \ Catalog \ ProductService.svc. And I got error 401.
'The remote server returned an error: (401) Unauthorized' is the result of invalid credentials. However, I tested the same credentials with the Client Api class, Through SecurityManager and get "UserLoggingReason.Succes", so the credentials are Correct.
The strange fact is that I do not have the ~ / Sitefinity / SWT folder. Maybe this is the root of my problems?
I am using ASP.NET MVC and I am making a request from Web Api Controller. And this is adapted code:
public static bool AuthenticateRequest(string membershipProvider, string userName, string password, bool rememberMe, ApiController controller) { var jsonData = String.Format(credentialsFormat, membershipProvider, userName, password, rememberMe.ToString().ToLower()); var credentials = Encoding.UTF8.GetBytes(jsonData); string result = InvokeWebMethod(usersServiceUrl, authenticateMethod, "POST", credentials, controller); switch (result) { case "0": return true; default: return false; } } public static string InvokeWebMethod(string serviceUrl, string methodName, string httpMethod, byte[] data, ApiController controller) { var request = (HttpWebRequest)WebRequest.Create(String.Concat(sitefinityHost, serviceUrl, methodName)); request.Method = httpMethod; request.ContentType = "application/json"; request.CookieContainer = new CookieContainer(); if (cookies != null) { foreach (Cookie cookie in cookies) if (!cookie.Expired) request.CookieContainer.Add(cookie); } if (data != null) { request.ContentLength = data.Length; using (var writer = request.GetRequestStream()) { writer.Write(data, 0, data.Length); } } using (var response = (HttpWebResponse)request.GetResponse())
(I also changed sitefinityHost to my machine)
Are all my 6 rooms fixed or has something changed?
What could be the cause of 401?
Thank you very much,
Links (most relevant):
[1] How to authenticate ( http://www.sitefinity.com/blogs/svetlayankova/posts/svetla-yankovas-blog/2011/11/01/getting_started_with_restful_services_in_sitefinity )
[2] How to authenticate ( http://www.sitefinity.com/blogs/svetla-yankovas-blog/2013/01/02/working-with-restful-services-part-2-claims-authentication-and-designing -service-calls )
[3] Authentication code ( http://www.sitefinity.com/developer-network/forums/general-discussions-/windows-authentication#1655610 )