简体   繁体   English

Azure 移动服务、HttpClient、授权

[英]Azure Mobile Services, HttpClient, Authorization

Can I use .NET's HttpClient to hit an Azure Mobile service?我可以使用 .NET 的 HttpClient 来访问 Azure 移动服务吗?

How do I authenticate with the Mobile Service's own baked in custom Authentication/Authorization patterns with the HttpClient?如何使用 HttpClient 在自定义身份验证/授权模式中使用移动服务自己的身份验证

This always returns 401, because I'm not passing in any authentication credentials:这总是返回 401,因为我没有传入任何身份验证凭据:

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

Furthermore, how come when I use the Mobile Service Client , why does my application key, master key, or user auth key always return (401) Unauthorized?此外,为什么当我使用Mobile Service Client 时,为什么我的应用程序密钥、主密钥或用户身份验证密钥总是返回 (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";
    }
}

"Can I use .NET's HttpClient to hit an Azure Mobile service?" “我可以使用 .NET 的 HttpClient 访问 Azure 移动服务吗?” The short answer is yes.简短的回答是肯定的。 The simple way is to add a this to the headers on the client:简单的方法是将 this 添加到客户端的标头中:

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

Be careful though, if you're using a locally hosted version you'll want to make sure you've...不过要小心,如果您使用的是本地托管版本,您需要确保您已经...

  1. Forced the service to think it's hosted so that it enables Authentication.强制服务认为它是托管的,以便启用身份验证。

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

  2. Added the application key and master key to the web.config:将应用程序密钥和主密钥添加到 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.如果没有 #1,跨服务的身份验证将被完全忽略,因此您不知道您在客户端中添加的身份验证是否正常工作。 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.如果没有 #2,您可以随心所欲地将密钥添加到客户端(从 Azure 获得),但它总是会返回 401。这可能是关于使用MobileServiceClient总是返回 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 :从这个MSDN 文档

  • X-ZUMO-APPLICATION -The application key of the mobile service. X-ZUMO-APPLICATION - 移动服务的应用密钥。 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. X-ZUMO-AUTH - 为经过身份验证的用户提供的服务生成的身份验证令牌。 You must specify a token for an authenticated user when required to access the table operation.当需要访问表操作时,您必须为经过身份验证的用户指定令牌。
  • X-ZUMO-MASTER - The service master key. X-ZUMO-MASTER - 服务主密钥。 You should only include this key when administrator access is required to access the table operation.只有在需要管理员访问权限才能访问表操作时,才应包含此键。

Author note: I personally struggled with getting this to work, and with either limited or missing documentation out there for this specific style, I wanted to write this Q/A.作者注:我个人一直在努力使其工作,并且由于这种特定风格的文档有限或缺失,我想写这个 Q/A。 Please let me know if you think I should add anything.如果您认为我应该添加任何内容,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM