Error 401 When calling Sitefinity WCF Web Services

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()) //The error is here { cookies = response.Cookies; using (var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) { var cookie = new HttpCookie("customCookie", "cookieVal") { Expires = DateTime.Now.AddDays(1), Domain = controller.Request.RequestUri.Host, Path = "/" }; HttpContext.Current.Response.SetCookie(cookie); return reader.ReadToEnd(); } } } 

(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 )

+5
source share
1 answer

timw255 wrote a REST client for Sitefinity. It is available here: https://github.com/timw255/timw255.Sitefinity.RestClient

The method below logs the user into it using RestSharp (a very reference library)

  private void SignIn() { RestRequest request = new RestRequest("Sitefinity/Authenticate", Method.GET); IRestResponse response = _restClient.Execute(request); switch (response.StatusCode) { case HttpStatusCode.OK: request = new RestRequest("Sitefinity/Authenticate/SWT?realm={realm}&redirect_uri={redirectUri}&deflate=true", Method.POST); request.AddUrlSegment("realm", _baseUrl); request.AddUrlSegment("redirectUri", "/Sitefinity"); request.AddParameter("wrap_name", _username, ParameterType.GetOrPost); request.AddParameter("wrap_password", _password, ParameterType.GetOrPost); request.AddParameter("sf_persistent", "true", ParameterType.GetOrPost); response = _restClient.Execute(request); switch (response.StatusCode) { case HttpStatusCode.OK: if (response.ResponseUri.AbsolutePath == "/Sitefinity/SignOut/selflogout") { SelfLogout(); } break; case HttpStatusCode.Unauthorized: throw new SitefinityException("Invalid username or password"); default: break; } break; case HttpStatusCode.Redirect: throw new NotImplementedException("External STS not supported"); default: break; } } 

File: https://github.com/timw255/timw255.Sitefinity.RestClient/blob/master/timw255.Sitefinity.RestClient/SitefinityRestClient.cs

The SWT folder is not the actual file system folder, this is the route.

0
source

All Articles