C# RESTSharp client connect Drupal Rest_server Access denied for user anonymous

我的未来我决定 提交于 2019-12-11 05:26:38

问题


I was search this article Consuming Drupal RestApi with c# And http://tylerfrankenstein.com/code/drupal-services-csrf-token-firefox-poster

I has question in the cookie and token. I have test in poster with firefox and successful on post the created article .Tamper Data has the request header.

tamper data

nid: "129342" uri: http://www.tsghy.com.cn/services/node/129342

Postman created the post code

                var client = new RestClient("http://www.tsghy.com.cn/services/node");
            var request = new RestRequest(Method.POST);
            request.AddHeader("postman-token", "5c28c9d6-d640-a4f0-a549-b6018e62907d");
            request.AddHeader("cache-control", "no-cache");
            request.AddHeader("x-csrf-token", "s0Z17LT7neX_K6grHgoJCUPR6VcL2QxRlNLmbRWeExE");
            request.AddHeader("content-type", "application/x-www-form-urlencoded");
            request.AddParameter("application/x-www-form-urlencoded", "type=article&title=test%201", ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);

I will crazy,

And replace to the C# ,i has Access denied for user anonymous.this my code with below : First I login the Drupal with Rest

private login_user2 loginAsync2(string username, string password)
    {
        try
        {
            RestClient client = new RestClient(base_url2);
            var request = new RestRequest("user/login.json", Method.POST);
            request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
            client.Authenticator = new SimpleAuthenticator("username",username,"password",password);
            var restResponse = client.Execute(request);
            var content = restResponse.Content;
            if (restResponse.StatusCode==System.Net.HttpStatusCode.OK)
            {
                login_user2 loginuser = JsonConvert.DeserializeObject<login_user2>(content.ToString());                    
                request = new RestRequest("session/token", Method.GET);
                restResponse = client.Execute(request);
                loginuser.session_token = restResponse.Content.ToString();
                return loginuser;
            }
            else {
                return null;
            }
        }
        catch (Exception ex) { throw ex; }
    }

I have question at the login/user->token and session/token ,which the difference?

Second,Post the create data:

        RestClient client = new RestClient(base_url2);
        var request = new RestRequest("node", Method.POST);
        request.AddHeader("cache-control", "no-cache"); 
        request.AddHeader("content-type", "application/json; charset=UTF-8");
        request.AddHeader("Accept", "application/json");
        request.AddHeader("cookie", "Drupal.toolbar.collapsed=0; "+current_user2.session_name+"="+current_user2.sessid+"; has_js=1");
        request.AddHeader("x-csrf-token",current_user2.session_token);
        request.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:48.0) Gecko/20100101 Firefox/48.0");
        request.AddParameter("application/json", myjobject, ParameterType.RequestBody);

        var queryresult = client.Execute(request);

回答1:


yes,I found the key,restful has bug at the request.addheader(cookie,cookie); change to :request.AddParameter(session_name,session_id,parametertype.cookie);



来源:https://stackoverflow.com/questions/39652010/c-sharp-restsharp-client-connect-drupal-rest-server-access-denied-for-user-anony

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!