OrchardCMS / OrchardCore

Orchard Core is an open-source modular and multi-tenant application framework built with ASP.NET Core, and a content management system (CMS) built on top of that framework.
https://orchardcore.net
BSD 3-Clause "New" or "Revised" License
7.42k stars 2.39k forks source link

Issue calling post method in xamrin forms #9216

Closed overstartup closed 3 years ago

overstartup commented 3 years ago

I have a module to Handel register users from Xamarin. It works fine by get, but it doesn't work by post methods.

i get this return:

{StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.HttpConnection+HttpConnectionResponseContent, Headers:
{
  Date: Sun, 18 Apr 2021 01:09:34 GMT
  Server: Kestrel
  X-Powered-By: OrchardCore
  Forwarded: host=192.168.0.3:45455; proto=https
  Content-Type: application/problem+json; charset=utf-8
  Content-Length: 163
}}

i have this code in xamarin side:

    public static async Task<HttpRequestMessage > PostStreamAsync(object content, string Url )
    {

        using (var request = new HttpRequestMessage(HttpMethod.Post, Url))
        using (var httpContent = CreateHttpContent(content))
        {
            request.Content = httpContent;

            var response = await App.client
                .SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
                .ConfigureAwait(false);

                response.EnsureSuccessStatusCode();

                return response.RequestMessage ;
        }
    }

and this is in orchard core side:

 [HttpPost]
        [Route("reg")]
        public async Task<IActionResult> RegisterInApp([FromBody] UserInformation RegisterVM)
        {
            try
            {
                if (RegisterVM == null)
                {
                    return BadRequest(ErrorCode.TodoItemNameAndNotesRequired.ToString());
                }

....
}
hishamco commented 3 years ago

Use HttpClient.PostAsync instead. One more thing why you didn't use OC APIs in users module

overstartup commented 3 years ago

HttpClient.PostAsync

i used it, i have same issue.

One more thing why you didn't use OC APIs in users module

How can i use it?

i thin it needs to add some CORS headers. i don't know what i should add. image

deanmarcussen commented 3 years ago

Probably you are missing an IgnoreAntiForgeryToken attribute on the controller / method.

hishamco commented 3 years ago

Probably you are missing an IgnoreAntiForgeryToken

May be

overstartup commented 3 years ago

@deanmarcussen @hishamco thank alot for your help. yes it needs IgnoreAntiForgeryToken . it works now