简体   繁体   English

通过Azure API Management Rest服务创建用户

[英]Create a user via the Azure API management Rest service

I'm new to Azure API management Rest Service. 我是Azure API Management Rest Service的新手。 I created a new API Management, with a sharedaccesstoken . 我创建了一个新的API管理,带有sharedaccesstoken

using (HttpClient httpClient = new HttpClient())
{
    var request = new HttpRequestMessage(HttpMethod.Post, requestUrl);
    request.Headers.Authorization =
    new AuthenticationHeaderValue("SharedAccessSignature", sharedAccessSignature);
    request.Content = new StringContent("{\"accountEnabled\": true,\"creationType\": \"LocalAccount\",\"displayName\": \"Alex Wu\",\"passwordProfile\": {\"password\": \"Test1234\",\"forceChangePasswordNextLogin\": false},\"signInNames\": [{\"type\": \"userName\",\"value\": \"AlexW\"},{\"type\": \"emailAddress\",\"value\": \"AlexW@example.com\"}]}");
    HttpResponseMessage response = await httpClient.SendAsync(request);
    response.EnsureSuccessStatusCode();
    responseBody = await response.Content.ReadAsStringAsync();
}

When i execute the code above i get a error: 当我执行上面的代码时,我得到一个错误:

{StatusCode: 405, ReasonPhrase: 'Method Not Allowed', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:{  Date: Wed, 30 Mar 2016 19:38:15 GMT  Content-Length: 73  Allow: GET  Content-Type: application/json; charset=utf-8}}

Can someone help me moving forward so I will be able to create new users via the REST service. 有人可以帮助我前进,以便我能够通过REST服务创建新用户。

I just read up on the API Reference specified in https://docs.microsoft.com/en-us/rest/api/apimanagement/user/createorupdate . 我只是阅读了https://docs.microsoft.com/zh-cn/rest/api/apimanagement/user/createorupdate中指定的API参考。

For starters, you should use HttpMethod.Put instead of Post. 对于初学者,您应该使用HttpMethod.Put而不是Post。 The request path should be base url + /users/{unique uid}. 请求路径应为基本url + / users / {unique uid}。 Also, from what I can tell, the attributes you are trying to pass are not available for this transaction. 另外,据我所知,您尝试传递的属性不适用于此事务。

If it's something else you're actually trying to accomplish then creating an Azure API Management User Entity, please advice and I'll try to guide you further. 如果您实际上要完成其他工作,请创建一个Azure API管理用户实体,请提出建议,我将尝试进一步指导您。

Your answer helped me indeed. 您的回答确实帮助了我。 Below the implementation which made me accomplish my task. 下面的实现使我完成了任务。

  string requestUrl = string.Format("{0}/users/{1}?api-version={2}", baseUrl, userGuid, apiVersion);
            string responseBody = null;
            try
            {
                using (HttpClient httpClient = new HttpClient())
                { 
 var request = new HttpRequestMessage(HttpMethod.Put, requestUrl);
                        request.Headers.Authorization =
                            new AuthenticationHeaderValue("SharedAccessSignature", sharedAccessSignature);
                        request.Content = new StringContent("{\"firstName\": \"MyFirstName\",\"lastName\": \"MyLastName\",\"email\": \"example@mail\",\"password\": \"Password;\",\"state\": \"active\"}", Encoding.UTF8,"application/json");
                        HttpResponseMessage response = await httpClient.SendAsync(request);`enter code here`
                        response.EnsureSuccessStatusCode();
                        responseBody = await response.Content.ReadAsStringAsync();
}
            }

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

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