question

Stesvis-5434 avatar image
0 Votes"
Stesvis-5434 asked Stesvis-5434 commented

Problem with iOS and header API versioning

We have implemented API versioning using headers, so in HttpClient on the mobile app code (Xamarin Forms), we do something like this:

_myHttpClient.DefaultRequestHeaders.Add(CustomRequestHeaders.ApiVersion, "2");

In the backend, we have a UsersController and a Users_v2Controller, and we need to consume the method GetUsersByRole.

UsersController:

[Authorize]
    [RoutePrefix("api/Users")]
    public class UsersController : BaseApiController
    {
        // ...

        [HttpGet]
        [Route("")]
        public IHttpActionResult GetUsersByRole([FromUri] int clientId, [FromUri] string role)
        {
            if (ApiVersion == "2")
                return RedirectToRoute("GetUsersByRole_v2", new { clientId = clientId, role = role });

                return Ok ("V1 results");
        }

        // ...

    }


Users_v2Controller:

[Authorize]
    [RoutePrefix("api/Users")]
    public class Users_v2Controller : BaseApiController
    {
        // ...

        [HttpGet]
        [Route("", Name = "GetUsersByRole_v2")]
        public IHttpActionResult GetUsersByRole([FromUri] int clientId, [FromUri] string role)
        {
                return Ok ("V2 results");
        }

        // ...

    }


Now, I am consuming the GET /api/users?clientId=1&role=Admin with ApiVersion=2 from android, iOS and Postman to double check:

  • Android: returns V2 results

  • Postman: returns V2 results

  • iOS: returns 401: unauthorized but if i set ApiVersion=1 it works and returns V1 results

What could the problem be? Why would iOS say i am unauthorized?



dotnet-xamarindotnet-aspnet-mvc
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, Stesvis-5434. The 401 error is related to the server side, it's not related to the code. Please check the error log about server side.

0 Votes 0 ·

Hi @JarvanZhang-MSFT I agree that 401 is just authentication not recognized by the server, but I can't understand why the same endpoint with the same token should return 401 only if the client is iOS...

0 Votes 0 ·

@JarvanZhang-MSFT I suspect that for some reason RedirectToRoute is not propagating the access token.
Is there a way to make sure the access token is propagated? Also GetUsersByRole_v2 needs authorization...

However, it's not an issue when the client is android or Postman, very weird.

0 Votes 0 ·

1.Have you added Authorization for the header? Try using HttpClient.DefaultRequestHeaders.Authorization property.

2.Please confirm that you've got the iOS app configured to use the httpClient handler.
https://docs.microsoft.com/en-us/xamarin/cross-platform/macios/http-stack?context=xamarin/ios

3.Similar issue cases you could refer to:
https://stackoverflow.com/questions/56826492/post-request-to-api-works-for-android-but-not-for-ios-xamarin
https://stackoverflow.com/questions/45468173/xamarin-forms-not-sending-authorization-header

0 Votes 0 ·
Show more comments

0 Answers