繁体   English   中英

使用Microsoft Graph时如何获取DeleteUser的状态 API.Net Sdk

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

我在这里查看此示例: 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();

它说:“如果成功,此方法返回 204 No Content 响应代码。它不会在响应正文中返回任何内容。”

但是,DeleteAsync() 方法的签名只是一个任务。 当我调用它时,我如何获得有关用户是否被删除的任何信息?

在发生错误的情况下, DeleteAsync将抛出ServiceException并带有包含服务错误详细信息的内部Error 如果调用成功,则表示该用户已被删除。 如文档中所述:如果成功,它不会在响应正文中返回任何内容。

如果您想检查请求返回什么状态代码以确保响应返回 204,您可以使用 .Net Microsoft Graph 客户端库发送 HTTP 请求。

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()
        });
}                

暂无
暂无

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

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