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, 2019 in Azure by sabby
• 4,390 points
1,106 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, 2019 by Prerna
• 1,960 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 22, 2018 in Azure by club_seesharp
• 3,450 points
3,451 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 11, 2018 in Azure by null_void
• 3,220 points
3,954 views
0 votes
1 answer

Can I provision an Azure service without using the portal?

Yes, It is possible to provision the ...READ MORE

answered Dec 27, 2019 in Azure by Sirajul
• 59,230 points
476 views
0 votes
1 answer

How to add a body to a HttpWebRequest that is being used with the Azure Service management API?

The following code should help: byte[] buf = ...READ MORE

answered Apr 3, 2019 in Azure by Prerna
• 1,960 points
2,030 views
0 votes
1 answer

Quering all the rows in Azure table Storage.

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

answered Apr 12, 2019 in Azure by Prerna
• 1,960 points
4,286 views
0 votes
1 answer
0 votes
2 answers

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

I had the same issue before, and ...READ MORE

answered Sep 17, 2019 in Azure by Ju Cheh
22,415 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, 2019 in Azure by Prerna
• 1,960 points
4,650 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP