Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET Core
  4. Integrating OneDrive API in ASP.NET Core Using Microsoft Graph: A Comprehensive Guide

Integrating OneDrive API in ASP.NET Core Using Microsoft Graph: A Comprehensive Guide

Date- May 02,2026 85

Overview

The OneDrive API, part of the Microsoft Graph API, provides a way to interact programmatically with OneDrive, Microsoft's cloud storage service. This API allows developers to access files stored in OneDrive, upload new files, manage user permissions, and synchronize changes across devices. The integration of OneDrive into applications addresses the need for scalable, resilient file storage solutions, enabling users to access their files from anywhere securely.

Real-world use cases for OneDrive API integration include enterprise applications that require document management, personal productivity tools that need file synchronization, and educational platforms offering collaborative workspaces. By leveraging OneDrive API, developers can create rich experiences that enhance user engagement while ensuring that data is securely stored and easily accessible.

Prerequisites

  • ASP.NET Core: Familiarity with building web applications using ASP.NET Core framework.
  • Microsoft Azure Account: Necessary for registering applications and obtaining API credentials.
  • Microsoft Graph API Knowledge: Basic understanding of how Microsoft Graph works and its RESTful nature.
  • Postman or Similar Tool: Useful for testing API requests independently of the application.

Setting Up Microsoft Graph API

Before you can integrate the OneDrive API, you need to set up the Microsoft Graph API in your Azure portal. This involves registering your application, which will generate the necessary credentials such as Client ID and Client Secret. This step is crucial as it allows your application to authenticate and interact with Microsoft services securely.

To register your application, navigate to the Azure portal, select 'Azure Active Directory', then 'App registrations', and click on 'New registration'. Enter your application name, redirect URI, and choose the appropriate account type. Once registered, note down your Client ID and Client Secret from the 'Certificates & secrets' section, as these will be used in your ASP.NET Core application.

// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
        .AddJwtBearer(options =>
        {
            options.Authority = "https://login.microsoftonline.com/{tenantId}/v2.0";
            options.Audience = "{clientId}";
        });
    services.AddHttpClient();
    services.AddControllers();
}

This code snippet configures the JWT Bearer authentication in the ASP.NET Core application. It specifies the authority for token validation and the audience that the tokens should be issued for. The HttpClient service is also registered for making HTTP requests to the Microsoft Graph API.

Understanding Authentication

Authentication is a critical aspect of integrating with the Microsoft Graph API. The API uses OAuth 2.0 for authorization, which allows users to grant your application permission to access their OneDrive data securely. You need to define the scopes required for your application, such as Files.ReadWrite for reading and writing files.

// Example of obtaining an access token
private async Task GetAccessTokenAsync()
{
    var client = new HttpClient();
    var request = new HttpRequestMessage(HttpMethod.Post, "https://login.microsoftonline.com/{tenantId}/oauth2/v2.0/token");
    var parameters = new Dictionary
    {
        { "client_id", "{clientId}" },
        { "client_secret", "{clientSecret}" },
        { "scope", "https://graph.microsoft.com/.default" },
        { "grant_type", "client_credentials" }
    };
    request.Content = new FormUrlEncodedContent(parameters);
    var response = await client.SendAsync(request);
    response.EnsureSuccessStatusCode();
    var json = await response.Content.ReadAsStringAsync();
    dynamic token = JsonConvert.DeserializeObject(json);
    return token.access_token;
}

This function demonstrates how to obtain an access token using client credentials. It constructs an HTTP POST request to the OAuth 2.0 token endpoint, passing the required parameters. Upon receiving a successful response, it extracts the access token for use in subsequent API calls.

Using OneDrive API to List Files

After obtaining the access token, you can start making requests to the OneDrive API. One common task is listing files in a user's OneDrive. This involves sending a GET request to the appropriate endpoint, which returns a collection of items stored in the user's OneDrive.

// Listing files in OneDrive
public async Task> ListFilesAsync(string accessToken)
{
    var client = new HttpClient();
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
    var response = await client.GetAsync("https://graph.microsoft.com/v1.0/me/drive/root/children");
    response.EnsureSuccessStatusCode();
    var json = await response.Content.ReadAsStringAsync();
    var driveItems = JsonConvert.DeserializeObject(json);
    return driveItems.Value;
}

This method sends a GET request to the OneDrive API to retrieve the items in the root folder of the user's drive. The access token is included in the Authorization header to authenticate the request. The response is deserialized into a custom DriveItemResponse class that holds a collection of DriveItem objects.

Handling DriveItem Response

The DriveItemResponse class must be defined to match the API response structure. This ensures that the deserialization process works correctly.

public class DriveItemResponse
{
    [JsonProperty("value")]
    public List Value { get; set; }
}

This class contains a single property, Value, which is a list of DriveItem objects, representing the files and folders in OneDrive. Each DriveItem contains properties such as Id, Name, and Size that provide details about each item.

Uploading Files to OneDrive

To enhance the functionality of your application, you may want to allow users to upload files to OneDrive. This process requires a different API endpoint and an HTTP PUT request to upload the file content.

// Uploading a file to OneDrive
public async Task UploadFileAsync(string accessToken, Stream fileStream, string fileName)
{
    var client = new HttpClient();
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
    var requestUri = $"https://graph.microsoft.com/v1.0/me/drive/root:/{fileName}:/content";
    var content = new StreamContent(fileStream);
    var response = await client.PutAsync(requestUri, content);
    response.EnsureSuccessStatusCode();
}

This method uploads a file to the user's OneDrive. The file stream is sent as the content of the PUT request. The constructed URI specifies the target location in OneDrive, which includes the file name. After the upload completes, it checks for success by calling EnsureSuccessStatusCode().

Error Handling for Uploads

It's essential to implement error handling when uploading files to manage scenarios such as file size limits and network issues. Implementing exception handling around the upload logic can provide better user feedback and ensure the application behaves correctly under various conditions.

try
{
    await UploadFileAsync(accessToken, fileStream, fileName);
}
catch (HttpRequestException ex)
{
    // Log error and notify the user
}

This code snippet demonstrates how to catch exceptions during the upload process and log any errors that occur. Notifying users about the failure can improve the overall user experience.

Edge Cases & Gotchas

When integrating with the OneDrive API, developers may encounter several edge cases and common pitfalls. One of the significant issues is handling rate limits imposed by the API. If your application makes too many requests in a short time, you may receive a 429 Too Many Requests error.

// Handling rate limit errors
if (response.StatusCode == (HttpStatusCode)429)
{
    var retryAfter = response.Headers.RetryAfter.Delta;
    // Implement backoff strategy
}

This code checks for a 429 status code and implements a backoff strategy using the RetryAfter header to wait before retrying the request. This approach helps avoid overwhelming the API and ensures compliance with usage policies.

Handling File Size Limits

Another common issue arises when uploading files that exceed OneDrive's size limits. The API allows for different upload methods depending on the file size, such as chunked uploads for large files. If a file exceeds the maximum size for a single upload, you must implement chunked uploading.

// Example for chunked file upload
// Pseudocode for handling large files
for (int i = 0; i < chunks; i++)
{
    await UploadChunkAsync(accessToken, chunkStream, fileName, i);
}

This pseudocode outlines a loop for uploading file chunks sequentially. Each chunk must be uploaded individually, and you should manage the state of each upload to ensure all parts are successfully transferred.

Performance & Best Practices

Optimizing the performance of your integration with OneDrive API is crucial for providing a smooth user experience. One of the best practices is to cache access tokens locally to reduce unnecessary authentication calls. This can significantly improve response times when making subsequent API requests.

// Caching access token
private string cachedToken;
private DateTime tokenExpiration;

private async Task GetCachedTokenAsync()
{
    if (cachedToken == null || DateTime.UtcNow >= tokenExpiration)
    {
        cachedToken = await GetAccessTokenAsync();
        tokenExpiration = DateTime.UtcNow.AddMinutes(55);
    }
    return cachedToken;
}

This caching mechanism checks if the cached token is still valid before requesting a new one, reducing the number of calls to the authentication endpoint.

Batch Requests

Another performance enhancement is using batch requests to minimize the number of HTTP requests made to the API. The Microsoft Graph API supports batch requests, allowing you to group multiple API calls into a single HTTP request.

// Example of batch request
var batchRequestContent = new BatchRequestContent();
var request1 = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/v1.0/me/drive/root/children");
var request2 = new HttpRequestMessage(HttpMethod.Get, "https://graph.microsoft.com/v1.0/me/drive/recentlyUsed");
batchRequestContent.AddBatchRequestStep(request1);
batchRequestContent.AddBatchRequestStep(request2);
var response = await client.SendBatchAsync(batchRequestContent);

This code snippet demonstrates how to create a batch request that retrieves both the root children and recently used files in one call, improving performance by reducing round trips to the server.

Real-World Scenario: Building a File Management System

As a practical example, let's build a simple ASP.NET Core application that allows users to authenticate via Microsoft, list their OneDrive files, and upload new files. This application will demonstrate the concepts discussed and provide a working example.

// Program.cs
public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }
    public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup();
        });
}

// Startup.cs
public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
            .AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"));
        services.AddHttpClient();
        services.AddControllersWithViews();
    }
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseRouting();
        app.UseAuthentication();
        app.UseAuthorization();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

// OneDriveController.cs
[Authorize]
public class OneDriveController : Controller
{
    public async Task Index()
    {
        var accessToken = await GetAccessTokenAsync();
        var files = await ListFilesAsync(accessToken);
        return View(files);
    }

    [HttpPost]
    public async Task Upload(IFormFile file)
    {
        using var stream = file.OpenReadStream();
        await UploadFileAsync(accessToken, stream, file.FileName);
        return RedirectToAction("Index");
    }
}

This code implements a simple ASP.NET Core application with a controller that lists files from OneDrive and allows file uploads. The Index action retrieves the user's files and displays them, while the Upload action handles file uploads using the methods discussed earlier.

Conclusion

  • Integrating OneDrive API into ASP.NET Core applications can enhance functionality and user experience.
  • Understanding authentication and API endpoints is crucial for successful integration.
  • Handling edge cases like rate limits and file size restrictions is essential for robust applications.
  • Implementing performance optimizations such as caching and batch requests can lead to significant improvements.
  • Real-world scenarios illustrate the practical application of these concepts in building feature-rich applications.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

How to Encrypt and Decrypt Password in Asp.Net
May 15, 2022
Exception Handling Asp.Net Core
Aug 05, 2020
HTTP Error 500.31 Failed to load ASP NET Core runtime
Aug 23, 2022
How to implement Paypal in Asp.Net Core
Oct 30, 2022
Previous in ASP.NET Core
Integrating Dropbox API with ASP.NET Core for Efficient File Sync…
Next in ASP.NET Core
Integrating Backblaze B2 Cloud Storage with ASP.NET Core Applicat…
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    Complete Guide to C++ Classes: Explained with Examples 4,212 views
  • 2
    Implementing an End-to-End CI/CD Pipeline for ASP.NET Core… 367 views
  • 3
    Create Database and CRUD operation 3,388 views
  • 4
    Mastering TypeScript Utility Types: Partial, Required, Rea… 675 views
  • 5
    Responsive Slick Slider 23,373 views
  • 6
    Integrating Azure Cognitive Search into ASP.NET Core Appli… 156 views
  • 7
    Integrating Anthropic Claude API in ASP.NET Core for AI Ch… 141 views
  • 8
    How to get fcm server key 4,849 views

On this page

🎯

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • Task Scheduler in Asp.Net core 17705 views
  • Implement Stripe Payment Gateway In ASP.NET Core 16931 views
  • Send Email With HTML Template And PDF Using ASP.Net C# 16634 views
  • How to implement Paypal in Asp.Net Core 8.0 13073 views
  • HTTP Error 502.5 - ANCM Out Of Process Startup Failure 12927 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1770
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor