How to Upload Files to OneDrive with Microsoft Graph in ASP.NET C#
Question details
The user needs to successfully upload files to OneDrive from an ASP.NET web application using the Microsoft Graph API and 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.
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.
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.
Navigate to your application registration in the Azure Portal. Go to API permissions, add Microsoft Graph, select Application permissions, and check 'Files.ReadWrite.All'.
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.
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.
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.

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. Download WPS Office: Visit the official WPS website and download the free installation package for your operating system.
- 2. Install the Application: Run the installer and follow the quick setup instructions to integrate WPS Office into your workflow.
- 3. Open Office Formats Instantly: Start opening, editing, and saving your existing Microsoft Office documents seamlessly.

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.




