简体   繁体   中英

how to get the status of DeleteUser when using Microsoft Graph API .Net Sdk

I am looking at this sample here: https://docs.microsoft.com/en-us/graph/api/user-delete?view=graph-rest-1.0&tabs=csharp

GraphServiceClient graphClient = new GraphServiceClient( authProvider );

await graphClient.Users["{user-id}"]
    .Request()
    .DeleteAsync();

It says: "If successful, this method returns 204 No Content response code. It does not return anything in the response body."

However, the signature for DeleteAsync() method is just a Task. When I call it, how can I get any information about whether the user was deleted or not?

In the case of an error, DeleteAsync will throw ServiceException with an inner Error that contains the service error details. If the call succeeds it means that the user was deleted. As mentioned in the doc: If successful, it does not return anything in the response body.

If you want to check what status code the request returns to be sure that response returns 204 you can send HTTP request using the.Net Microsoft Graph client library.

var requestUrl = client.Users["{user-id}"].Request().RequestUrl;

// create the request message
var request = new HttpRequestMessage(HttpMethod.Delete, requestUrl);

// authenticate HttpRequestMessage
await client.AuthenticationProvider.AuthenticateRequestAsync(request);
// it will call your IAuthenticationProvider you specified in ctor of GraphClient class
// and add Authorization header with access token to the request

// send the request and get the response
var response = await client.HttpProvider.SendAsync(request);

// read status code response.StatusCode
if (!response.IsSuccessStatusCode)
{
    throw new ServiceException(
        new Error
        {
            Code = response.StatusCode.ToString(),
            // read details why the user was not deleted
            Message = await response.Content.ReadAsStringAsync()
        });
}                

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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