.NET幫助 MS Graph .NET(對於開發者的運行原理) Jacob Mellor 更新:2025年6月22日 下載 IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 開始免費試用 LLM副本 LLM副本 將頁面複製為 Markdown 格式,用於 LLMs 在 ChatGPT 中打開 請向 ChatGPT 諮詢此頁面 在雙子座打開 請向 Gemini 詢問此頁面 在 Grok 中打開 向 Grok 詢問此頁面 打開困惑 向 Perplexity 詢問有關此頁面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 複製連結 電子郵件文章 MS Graph .NET用作存取資料的工具,用於與 Microsoft Graph API 進行交互,Microsoft Graph API 是Azure Active Directory (Azure AD) 生態系統的核心部分。 Microsoft Graph 是 Microsoft 365 中資料和智慧的入口。它允許開發人員存取、管理和分析各種 Microsoft 服務中的資料。 Microsoft Graph 用戶端程式庫提供了一系列與 API 輕鬆互動的方法,從而簡化了這個過程。 IronPDF是一個用於在.NET應用程式中產生 PDF 文件的程式庫。 它可以將 HTML 轉換為 PDF ,從而可以自動建立報告、發票和文件。 IronPDF與.NET應用程式配合得很好,提供了簡單直接的 PDF 產生方法。 將 MS Graph .NET和IronPDF結合使用,可讓開發人員建立能夠操作 Microsoft 365 資料並產生 PDF 文件的應用程式。 這種組合對於開發需要從 Microsoft 服務獲取資料並以標準化文件格式呈現這些資料的業務應用程式來說非常強大。 MS Graph .NET入門指南 在.NET專案中設定 MS Graph .NET 要有效使用 MS Graph .NET ,尤其是在.NET Core專案中處理使用者 ID時,第一步是設定您的.NET專案。以下是具體步驟: 開啟NuGet套件管理器。 搜尋Microsoft.Graph 。 安裝Microsoft.Graph程式包。 此過程會將 MS Graph .NET加入您的專案中。 現在,你可以開始用它寫程式了。 基本程式碼範例 假設你想取得目前使用者的個人資料資訊。 以下是一個簡單的程式碼範例: // Required namespaces using Azure.Identity; using Microsoft.Graph; // Defining necessary credentials and scope var clientId = "Your_Application_Id"; var tenantId = "Your_Tenant_Id"; var clientSecret = "Your_Client_Secret"; var scopes = new[] { "User.Read" }; // Configuring TokenCredentialOptions for Azure Public Cloud var options = new TokenCredentialOptions { AuthorityHost = AzureAuthorityHosts.AzurePublicCloud }; // Authenticating using client credentials var clientSecretCredential = new ClientSecretCredential( tenantId, clientId, clientSecret, options); // Creating a new instance of GraphServiceClient var graphClient = new GraphServiceClient(clientSecretCredential, scopes); // Fetching current user's profile information var user = await graphClient.Me .Request() .GetAsync(); // Printing user's display name Console.WriteLine($"Hello, {user.DisplayName}!"); // Required namespaces using Azure.Identity; using Microsoft.Graph; // Defining necessary credentials and scope var clientId = "Your_Application_Id"; var tenantId = "Your_Tenant_Id"; var clientSecret = "Your_Client_Secret"; var scopes = new[] { "User.Read" }; // Configuring TokenCredentialOptions for Azure Public Cloud var options = new TokenCredentialOptions { AuthorityHost = AzureAuthorityHosts.AzurePublicCloud }; // Authenticating using client credentials var clientSecretCredential = new ClientSecretCredential( tenantId, clientId, clientSecret, options); // Creating a new instance of GraphServiceClient var graphClient = new GraphServiceClient(clientSecretCredential, scopes); // Fetching current user's profile information var user = await graphClient.Me .Request() .GetAsync(); // Printing user's display name Console.WriteLine($"Hello, {user.DisplayName}!"); $vbLabelText $csharpLabel 此程式碼片段示範如何使用用戶端金鑰進行 Azure AD 驗證來建立 GraphServiceClient 的新執行個體。 它使用客戶端憑證進行身份驗證。 然後,它檢索目前使用者的顯示名稱。 接下來,請確保將 MS Graph .NET以及驗證提供者設定新增至您的專案。 MS Graph .NET的功能 檢索使用者電子郵件 若要從使用者的 Microsoft 帳戶信箱擷取電子郵件,您可以使用Mail.Read權限。 以下是列出最新電子郵件的方法: // Retrieving the top 10 messages from the user's mailbox var messages = await graphClient.Me.Messages .Request() .Top(10) .GetAsync(); // Iterating through the messages and printing their subjects foreach (var message in messages) { Console.WriteLine($"Subject: {message.Subject}"); } // Retrieving the top 10 messages from the user's mailbox var messages = await graphClient.Me.Messages .Request() .Top(10) .GetAsync(); // Iterating through the messages and printing their subjects foreach (var message in messages) { Console.WriteLine($"Subject: {message.Subject}"); } $vbLabelText $csharpLabel 這段程式碼列出了用戶收件匣中前 10 封郵件的主題。 傳送電子郵件 發送電子郵件涉及建立Message物件並將其發送: // Creating a message to be sent var message = new Message { Subject = "Hello from MS Graph .NET", Body = new ItemBody { ContentType = BodyType.Text, Content = "Hello, this is a test email." }, ToRecipients = new List<Recipient>() { new Recipient { EmailAddress = new EmailAddress { Address = "recipient@example.com" } } } }; // Sending the email await graphClient.Me.SendMail(message, null) .Request() .PostAsync(); // Creating a message to be sent var message = new Message { Subject = "Hello from MS Graph .NET", Body = new ItemBody { ContentType = BodyType.Text, Content = "Hello, this is a test email." }, ToRecipients = new List<Recipient>() { new Recipient { EmailAddress = new EmailAddress { Address = "recipient@example.com" } } } }; // Sending the email await graphClient.Me.SendMail(message, null) .Request() .PostAsync(); $vbLabelText $csharpLabel 這將發送一封內容僅為純文字的電子郵件。 管理日曆事件 若要將事件新增至使用者的日曆: // Creating a calendar event var @event = new Event { Subject = "Team Meeting", Body = new ItemBody { ContentType = BodyType.Html, Content = "Discuss project updates." }, Start = new DateTimeTimeZone { DateTime = "2024-04-15T12:00:00", TimeZone = "Pacific Standard Time" }, End = new DateTimeTimeZone { DateTime = "2024-04-15T14:00:00", TimeZone = "Pacific Standard Time" }, Location = new Location { DisplayName = "Conference Room 1" } }; // Adding the event to the user's calendar await graphClient.Me.Events .Request() .AddAsync(@event); // Creating a calendar event var @event = new Event { Subject = "Team Meeting", Body = new ItemBody { ContentType = BodyType.Html, Content = "Discuss project updates." }, Start = new DateTimeTimeZone { DateTime = "2024-04-15T12:00:00", TimeZone = "Pacific Standard Time" }, End = new DateTimeTimeZone { DateTime = "2024-04-15T14:00:00", TimeZone = "Pacific Standard Time" }, Location = new Location { DisplayName = "Conference Room 1" } }; // Adding the event to the user's calendar await graphClient.Me.Events .Request() .AddAsync(@event); $vbLabelText $csharpLabel 這段程式碼會在日曆中安排一個新事件。 存取 OneDrive 文件 若要列出使用者 OneDrive 根目錄下的檔案: // Retrieving files from the root OneDrive folder var files = await graphClient.Me.Drive.Root.Children .Request() .GetAsync(); // Printing each file's name foreach (var file in files) { Console.WriteLine(file.Name); } // Retrieving files from the root OneDrive folder var files = await graphClient.Me.Drive.Root.Children .Request() .GetAsync(); // Printing each file's name foreach (var file in files) { Console.WriteLine(file.Name); } $vbLabelText $csharpLabel 這段程式碼會列印出 OneDrive 根目錄中的檔案名稱。 團隊協作 若要取得使用者所屬的團隊清單: // Retrieving teams that the user is part of var teams = await graphClient.Me.JoinedTeams .Request() .GetAsync(); // Printing each team's display name foreach (var team in teams) { Console.WriteLine($"Team name: {team.DisplayName}"); } // Retrieving teams that the user is part of var teams = await graphClient.Me.JoinedTeams .Request() .GetAsync(); // Printing each team's display name foreach (var team in teams) { Console.WriteLine($"Team name: {team.DisplayName}"); } $vbLabelText $csharpLabel 此清單顯示使用者所屬的團隊名稱。 這些特性都體現了 MS Graph .NET的強大功能和多功能性。 它們展示瞭如何將 Microsoft 365 服務整合到您的應用程式中。 將 MS Graph .NET與IronPDF集成 如果您想在.NET應用程式中處理 PDF 文件, IronPDF Library for .NET Developers是一個不錯的選擇。該程式庫使您的應用程式能夠讀取、建立和操作 PDF 文件,而無需任何其他外部 PDF 工具或軟體。 使用案例:將IronPDF與 MS Graph .NET合併 想像一下,你正在建立一個應用程序,需要從 Microsoft 365 中取得文件(例如報告或發票),並將其轉換為 PDF。 MS Graph .NET可讓您與 Microsoft 365 資源交互,包括儲存在 OneDrive 或 SharePoint 中的檔案。 然後可以使用IronPDF將這些文件轉換為 PDF 格式。 這種組合對於自動產生報告或將電子郵件和附件存檔為 PDF 格式以便於分發尤其有用。 程式碼範例:從 MS 圖形轉換為 PDF 我們來看一個簡單的例子。 我們將使用 MS Graph .NET從 OneDrive 取得文檔,然後使用IronPDF將該文檔轉換為 PDF。 我假設您已經使用 MSGraph 設定了身份驗證; 如果還沒找到合適的資料,微軟網站上有許多文件可以幫助你入門。 // Simplified example, ensure to handle exceptions and errors appropriately. using Microsoft.Graph; using IronPdf; using System.IO; using System.Threading.Tasks; class Program { static async Task Main(string[] args) { // Setting up the GraphServiceClient with a DelegateAuthenticationProvider var graphClient = new GraphServiceClient( new DelegateAuthenticationProvider( async (requestMessage) => { // Insert code to acquire token string accessToken = await GetAccessTokenAsync(); requestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken); })); // Replace 'itemId' with the ID of your document in OneDrive var stream = await graphClient.Me.Drive.Items["itemId"].Content.Request().GetAsync(); // IronPDF setup to convert the fetched file to PDF var renderer = new HtmlToPdf(); var pdfDocument = renderer.RenderHtmlAsPdf(StreamToString(stream)); // Save the PDF to a file pdfDocument.SaveAs("YourDocument.pdf"); } // Method to convert a Stream to a String static string StreamToString(Stream stream) { using (var reader = new StreamReader(stream)) { return reader.ReadToEnd(); } } // Method to obtain the access token static async Task<string> GetAccessTokenAsync() { // Implement your authentication logic here to return the access token return "your_access_token"; } } // Simplified example, ensure to handle exceptions and errors appropriately. using Microsoft.Graph; using IronPdf; using System.IO; using System.Threading.Tasks; class Program { static async Task Main(string[] args) { // Setting up the GraphServiceClient with a DelegateAuthenticationProvider var graphClient = new GraphServiceClient( new DelegateAuthenticationProvider( async (requestMessage) => { // Insert code to acquire token string accessToken = await GetAccessTokenAsync(); requestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken); })); // Replace 'itemId' with the ID of your document in OneDrive var stream = await graphClient.Me.Drive.Items["itemId"].Content.Request().GetAsync(); // IronPDF setup to convert the fetched file to PDF var renderer = new HtmlToPdf(); var pdfDocument = renderer.RenderHtmlAsPdf(StreamToString(stream)); // Save the PDF to a file pdfDocument.SaveAs("YourDocument.pdf"); } // Method to convert a Stream to a String static string StreamToString(Stream stream) { using (var reader = new StreamReader(stream)) { return reader.ReadToEnd(); } } // Method to obtain the access token static async Task<string> GetAccessTokenAsync() { // Implement your authentication logic here to return the access token return "your_access_token"; } } $vbLabelText $csharpLabel 這段程式碼有幾點要注意: 我們正在使用 MSGraph 存取儲存在 OneDrive 中的檔案。您需要該檔案的 ID,可以透過 Graph API 取得。 在本範例中,我們將檔案流轉換為字串。 這對於HTML文件來說效果很好。 如果您處理的是二進位(例如 Word 文件),則需要使用不同的方法將這些文件轉換為 PDF。 這裡使用IronPDF的RenderHtmlAsPdf方法從 HTML 字串建立 PDF。 如果您的來源文件不是 HTML 格式, IronPDF也提供了處理其他格式的方法。 請記住,這只是一個簡化的例子。 在實際應用中,你需要更穩健地處理身份驗證,管理錯誤,並可能更優雅地處理不同的文件格式。 但這應該能為你在.NET專案中整合 MSGraph 和IronPDF提供一個好的起點。 結論 對於希望將 Microsoft 365 功能整合到其 C# 應用程式中的開發人員來說,MS Graph .NET SDK 是必不可少的工具。 首先,請從 $799 開始探索MS Graph .NET SDK 授權和定價資訊。 常見問題解答 MS Graph .NET 如何幫助開發人員訪問 Microsoft 365 數據? MS Graph .NET 作為開發人員訪問和管理 Microsoft 365 數據的門戶,通過 Azure Active Directory 生態系統的一部分,提供簡化數據互動過程的方法。 開發人員如何在 .NET 中使用 Microsoft 365 數據生成 PDF 文檔? 開發人員可以使用 IronPDF 將 HTML 和其他文檔格式轉換為 PDF,從而在 .NET 應用程序中生成 PDF 文檔,利用通過 MS Graph .NET 訪問的數據。 開始在項目中使用 MS Graph .NET 需要什麼? 要開始使用 MS Graph .NET,您需要通過 NuGet 包管理器安裝 Microsoft.Graph 包,並設置使用客戶端憑證進行身份驗證以與 Microsoft 365 服務互動。 開發人員如何使用 MS Graph .NET 發送電子郵件? 開發人員可以通過創建帶有所需內容和接收者詳細信息的 `Message` 對象,然後利用 `GraphServiceClient` 的 `SendMail` 方法來使用 MS Graph .NET 發送電子郵件。 MS Graph .NET 可以管理 Microsoft 365 帳戶中的日曆事件嗎? 是的,MS Graph .NET 允許通過創建 `Event` 對象和使用例如 `Me.Events.Request().AddAsync(event)` 這樣的方法來添加事件到用戶的日曆中來管理日曆事件。 如何在 .NET 應用程序中將 OneDrive 文檔轉換為 PDF? 要將 OneDrive 文檔轉換為 PDF,可以使用 MS Graph .NET 獲取文檔,然後使用 IronPDF 將文檔內容轉換為 PDF 格式。 將 MS Graph .NET 與 IronPDF 整合時應考慮哪些因素? 在將 MS Graph .NET 與 IronPDF 整合時,考慮強健的身份驗證、錯誤處理以及不同文件格式的兼容性,以確保無縫轉換和數據管理。 將 MS Graph .NET 與 IronPDF 結合使用的一些實際應用是什麼? 結合使用 MS Graph .NET 和 IronPDF 可以生成 PDF 報告、將郵件存檔為 PDF,或從 Microsoft 365 數據創建標準化業務文檔。 MS Graph .NET 如何提高 .NET 應用程序的效率? MS Graph .NET 通過提供簡化的 Microsoft 365 服務訪問來提高效率,使開發人員能夠以最少的代碼檢索、管理和操作數據,從而提高生產力和應用程序的能力。 Jacob Mellor 立即與工程團隊聊天 首席技術官 Jacob Mellor是Iron Software的首席技術官,也是開創C# PDF技術的前瞻性工程師。作為Iron Software核心代碼庫的原始開發者,他自公司成立以來就塑造了公司的產品架構,並與CEO Cameron Rimington將公司轉型為服務NASA、Tesla以及全球政府機構的50多人公司。Jacob擁有曼徹斯特大學土木工程一級榮譽學士學位(1998年–2001年)。他於1999年在倫敦開立首家軟體公司,並於2005年建立了他的第一個.NET組件,專注於解決Microsoft生態系統中的複雜問題。他的旗艦作品IronPDF和Iron Suite .NET程式庫全球已獲得超過3000萬次NuGet安裝,他的基礎代碼不斷在全球各地驅動開發者工具。擁有25年以上的商業經驗和41年的編碼專業知識,Jacob仍然專注於推動企業級C#、Java和Python PDF技術的創新,同時指導下一代技術領導者。 相關文章 更新2026年2月20日 銜接 CLI 簡化與 .NET : 使用 Curl DotNet 與 IronPDF for .NET Jacob Mellor 藉由 CurlDotNet 彌補了這方面的不足,CurlDotNet 是為了讓 .NET 生態系統能熟悉 cURL 而建立的函式庫。 閱讀更多 更新2025年12月20日 RandomNumberGenerator C# 使用RandomNumberGenerator C#類可以幫助將您的PDF生成和編輯項目提升至新水準 閱讀更多 更新2025年12月20日 C#字符串等於(它如何對開發者起作用) 當結合使用強大的PDF庫IronPDF時,開關模式匹配可以讓您構建更智能、更清晰的邏輯來進行文檔處理 閱讀更多 Livecharts C#(對於開發者的運行原理)C# Ref(對於開發者的運行...
更新2026年2月20日 銜接 CLI 簡化與 .NET : 使用 Curl DotNet 與 IronPDF for .NET Jacob Mellor 藉由 CurlDotNet 彌補了這方面的不足,CurlDotNet 是為了讓 .NET 生態系統能熟悉 cURL 而建立的函式庫。 閱讀更多