Can I use .NET's HttpClient to hit an Azure Mobile service?

0 votes

When I try to authenticate with the Mobile Service's own baked in custom Authentication/Authorization patterns with the HttpClient, it always returns error 401, because I'm not passing in any authentication credentials:

var client = new HttpClient();           
var response = client.GetAsync("http://localhost:49190/api/test").Result;

Also, when I use the Mobile Service Client, why does my application key, master key, or user auth key always return (401) Unauthorized?

Client:

var mobileClient = new MobileServiceClient("http://localhost:49190/", "[my key]");
var response = mobileClient.InvokeApiAsync("test").Result;

Service Side:

[AuthorizeLevel(AuthorizationLevel.Application)]
public class TestController : ApiController
{
    public ApiServices Services { get; set; }

    // GET api/Test
    public string Get()
    {
        Services.Log.Info("Hello from custom controller!");
        return "Hello";
    }
}
Apr 15 in Azure by sabby
• 4,350 points
60 views

1 answer to this question.

0 votes

Yes, you can do this by adding this to the headers on the client:

        var client = new HttpClient();           
        client.DefaultRequestHeaders.Add("X-ZUMO-APPLICATION", "[my key]");

Remember -

  1. Force the service to think it's hosted so that it enables Authentication.

    (in App_Start/WebApiConfig.cs: config.SetIsHosted(true);

  2. Add the application key and master key to the web.config:

    <appSettings>
    <add key="MS_MasterKey" value="[your master key]" />
    <add key="MS_ApplicationKey" value="[your app key]" />
    </appSettings>

Without #1, the authentication across the service will be completely ignored, and therefore you don't know if how you've added authentication in the client is working.

Without #2, you can add the key to the client (that you get from Azure) all you want, but it will always return 401. This may be the answer to the second question posted about using the MobileServiceClient always returning 401.

Lastly, there are three different headers you can use in total. You use each one with each different level of authorization. From this MSDN doc:

  • X-ZUMO-APPLICATION -The application key of the mobile service. You must specify a valid application key when required to access the table operation. This is the default table operation access permission.
  • X-ZUMO-AUTH - The service-generated authentication token for an authenticated user. You must specify a token for an authenticated user when required to access the table operation.
  • X-ZUMO-MASTER - The service master key. You should only include this key when administrator access is required to access the table operation.

answered Apr 15 by Prerna
• 1,940 points

Related Questions In Azure

0 votes
1 answer

How can I remove/hide/disable excessive HTTP response headers in Azure/IIS7 without having to use UrlScan?

MSDN published an article on how to ...READ MORE

answered May 21, 2018 in Azure by club_seesharp
• 3,450 points
360 views
0 votes
1 answer

How can I use “Azure File Storage” with Web App Service?

If you're looking for mapping a drive ...READ MORE

answered Aug 10, 2018 in Azure by null_void
• 3,220 points
999 views
+1 vote
2 answers

How do I establish an connection to Azure IoT Hub? Say a connection like MQTT/AMQP using Python.

Azure IoT Hub comes with 3 SDK's: Service SDK Device ...READ MORE

answered Apr 13, 2018 in Azure by null_void
• 3,220 points
489 views
0 votes
2 answers

How can I download a .vhd image to my local machine from azure and upload the same to a different azure account?

From the Windows Azure Portal you can ...READ MORE

answered Aug 20, 2018 in Azure by Priyaj
• 56,160 points
1,097 views
0 votes
1 answer
0 votes
1 answer

Quering all the rows in Azure table Storage.

Refer the following code: var acc = new ...READ MORE

answered Apr 12 in Azure by Prerna
• 1,940 points
11 views
0 votes
1 answer
0 votes
1 answer

I am getting "Insufficient Privileges" error when trying to access Azure Graph APIs.

This error is occurring because privileges are ...READ MORE

answered Jul 1 in Azure by Perry
• 17,010 points
24 views
0 votes
1 answer
0 votes
1 answer

Can I use an appsettings.json file in Azure Functions?

Only environment variables are supported for app ...READ MORE

answered Apr 24 in Azure by Prerna
• 1,940 points
108 views