繁体   English   中英

错误401调用Sitefinity WCF Web服务时

[英]Error 401 When calling the Sitefinity WCF Web Services

我已经开始使用Sitefinity 8.1进行开发,我需要访问Sitefinity的WCF Web服务(例如〜\\ Sitefinity \\ Services \\ Ecommerce \\ Catalog \\ ProductService.svc)

我试图像任何其他Web服务一样访问它们,但我收到401错误。 在网上搜索和Sitefinity论坛后,我发现了一些事情。

  1. 我需要在使用服务之前进行身份验证[1和2]

  2. 基于声明的身份验证是默认身份验证

  3. 用于验证的URL是/Sitefinity/Services/Security/Users.svc/authenticate [1&2]

  4. 我还找到了Ivan Dimitrov提供的一个片段,他在那里编码认证码[3]

  5. 客户端Api无需进行身份验证并允许对Web服务的请求

  6. 它需要一个STS进行身份验证,它集成在我的Sitefinity安装中[2] “你可能想知道这个STS在哪里。 默认情况下,逻辑集成在Sitefinity应用程序中,可以在〜/ Sitefinity / SWT下找到。 [2]

    在我阅读此信息后,我调整了Ivan Dimitrov [3]提供的代码,并将调用编码为〜\\ Sitefinity \\ Services \\ Ecommerce \\ Catalog \\ ProductService.svc。 我得到401错误。

'远程服务器返回错误:(401)Unauthorized'是由于凭据错误导致的,但是我使用Client Api测试了相同的凭据,通过SecurityManager类,我获得了“UserLoggingReason.Succes”,因此凭据是正确的。

奇怪的事实是我没有任何〜/ Sitefinity / SWT文件夹。 那可能是我问题的根源吗?

我正在使用ASP.NET MVC,我正在从Web Api控制器执行请求。 这是改编的代码:

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

(我还将sitefinityHost更改为我的机器)

我的所有6个场所都是正确的还是有些变化了?

可能是401的原因是什么?

非常感谢你,

参考文献(最相关的):

[1]如何进行身份验证http://www.sitefinity.com/blogs/svetlayankova/posts/svetla-yankovas-blog/2011/11/01/getting_started_with_restful_services_in_sitefinity

[2]如何进行身份验证http://www.sitefinity.com/blogs/svetla-yankovas-blog/2013/01/02/working-with-restful-services-part-2-claims-authentication-and-designing - 服务电话

[3]验证码http://www.sitefinity.com/developer-network/forums/general-discussions-/windows-authentication#1655610

timw255为Sitefinity编写了一个REST客户端。 它可以在这里找到: https//github.com/timw255/timw255.Sitefinity.RestClient

下面的方法记录用户正在使用RestSharp(非常帮助库)

 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;
        }
    }

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

SWT文件夹不是它是路由的实际文件系统文件夹。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM