Upload a file on one drive using microsoft graph api in c# asp.net

Alisha Mulla 0 Reputation points
2023-02-02T06:06:01.9066667+00:00

Hello,

I have one question,I want to upload a file on one drive and it is worked properly in console application but not  in web application. Please help me

here below is my sample console application code.

<--------------------------------------------------------------------------------------->

 var clientID = "
            var clientSec = "****";
            var tenant = "****";
            var userID = "***";

            var filePath = @"C:\onedrivedoc\LRN DRAFT retail (1).docx";
            var fileName = "LRN DRAFT retail (1).docx";

            IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
                .Create(clientID)
                .WithTenantId(tenant)
                .WithClientSecret(clientSec)
                .Build();

            ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);
            var graphClient = new GraphServiceClient(authProvider);

            StreamReader reader = System.IO.File.OpenText(filePath);

            var uploadedFile = graphClient.Users[userID].Drive.Root.ItemWithPath(fileName).Content.Request().PutAsync
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
6,431 questions
ASP.NET Web Forms
ASP.NET Web Forms
A part of the ASP.NET web application framework that can be used to create ASP.NET web applications.
458 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Carlos Solís Salazar 10,971 Reputation points
    2023-02-07T13:25:23.56+00:00

    To grant Permission for your App registration, you require to follow the next steps:

    1. In the navigation pane, click API permissions.
    2. Click Add a permission. The Request API permissions pane appears.
    3. Click Microsoft Graph tile.
    4. Click Application permissions.
    5. Select the following permissions:
      • Directory: Directory.Read.All
      • Files: Files.ReadWrite.All
      • User: User.Read.All
      • Notes: Notes.ReadWrite.All
      • Application: Application.ReadWrite.All
    6. Click Add permissions.
    7. Click Add a permission. The Request API permissions pane appears.
    8. Click Grant admin consent for tenant_name.
    9. Click Yes.

    Hope this helps!


    Accept Answer and Upvote, if any of the above helped, this thread can help others in the community looking for remediation for similar issues.

    NOTE: To answer you as quickly as possible, please mention me in your reply.