簡體   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