logo
search
OneDrive File Issues

How to Upload Files to OneDrive with Microsoft Graph in ASP.NET C#

Steve KSteve K Sep 29, 2026 868 views

Question details

The user needs to successfully upload files to OneDrive from an ASP.NET web application using the Microsoft Graph API and C#.

How to Upload Files to OneDrive with Microsoft Graph in ASP.NET C#
Product
OneDrive and Microsoft Graph API
Device & OS
not provided
Scenario
Implementing or migrating OneDrive file upload functionality within an ASP.NET web application environment using C#.
Observed behavior
Upload code that works in a console application fails in ASP.NET due to mismatched authentication flows, lack of administrator consent for application permissions, or restricted file stream access.
Before you start

Ensure you have registered your application in the Microsoft Entra ID (Azure AD) portal and have your Client ID, Tenant ID, and Client Secret ready for authentication.

Solution 1Recommended

Configure Application Permissions and Implement File Upload

Set up client-credentials authentication, assign the correct application permissions, and ensure proper stream handling in your ASP.NET application to resolve upload failures.

When uploading files to a user's OneDrive from a background service or web application, the application typically runs without a signed-in user context. This requires implementing the Client Credentials authentication flow and requesting application-level permissions rather than delegated permissions.

1
Configure permissions in the Azure Portal

Navigate to your application registration in the Azure Portal. Go to API permissions, add Microsoft Graph, select Application permissions, and check 'Files.ReadWrite.All'.

2
Grant administrator consent

After adding the necessary permissions, click the 'Grant admin consent' button for your tenant. This authorizes the ASP.NET application to upload files to OneDrive without requiring manual user login.

3
Verify local file access for ASP.NET

Check your IIS or ASP.NET application pool identity. Ensure that the running service account has the necessary read permissions for the local directory where the file to be uploaded is stored.

4
Implement the upload stream in C#

In your C# code, use a 'FileStream' to open the file. Pass this stream directly to the Microsoft Graph upload request. Ensure the stream is properly initialized and correctly passed to the GraphServiceClient to avoid file-locking issues.

Configure Application Permissions and Implement File Upload
Security Best Practice: Always adhere to the principle of least privilege. Avoid granting unrelated high-privilege permissions to your Azure application if they are not strictly required for the file upload process.
Free Microsoft Office alternative

Looking for a Seamless Office Experience? Try WPS Office

While developing and troubleshooting applications using Microsoft Graph and OneDrive, you might also need a reliable, lightweight office suite for your daily document tasks. WPS Office provides a powerful, free alternative to Microsoft Office with high compatibility and seamless cloud integration.

  1. 1. Download WPS Office: Visit the official WPS website and download the free installation package for your operating system.
  2. 2. Install the Application: Run the installer and follow the quick setup instructions to integrate WPS Office into your workflow.
  3. 3. Open Office Formats Instantly: Start opening, editing, and saving your existing Microsoft Office documents seamlessly.
Fully compatible with Microsoft Word, Excel, and PowerPoint formatsFree and lightweight Office suite that won't slow down your systemBuilt-in cloud support for easy file management across multiple devicesFamiliar user interface for a zero learning curve and seamless migration
microsoft office alternative - wps office

Frequently Asked Questions

Why does my Microsoft Graph upload code work in a console app but fail in ASP.NET?

Console applications often run under your personal user account with delegated permissions. ASP.NET web apps run under a specific application pool identity and typically use the Client Credentials flow, requiring Application permissions and explicit admin consent in Azure AD.

Which Graph API permissions are required to upload files to OneDrive via an app?

For uploading files using application permissions (Client Credentials flow), you generally need the 'Files.ReadWrite.All' permission, which must be granted admin consent by a tenant administrator.

How do I handle file streams correctly for Graph API uploads in ASP.NET?

Ensure the file stream is fully opened using 'File.OpenRead()' and properly passed into the Graph API upload request. Avoid locking issues common in ASP.NET environments by ensuring the stream is properly disposed of or wrapped in a 'using' statement.