I'm new to Sharepoint. Is it possible to upload files to an aspx page using c# and .NET Framework 4.7? The site I'm trying to upload to is "https://myorg.sharepoint.com/sites/SPODemo/Test%20Upload/Forms/AllItems.aspx" but any sample code I've seen uses a standard url rather than an aspx page. The SP admin person created this site for us in SP so all I have is the aspx page.

I've tried several variations of "https://myorg.sharepoint.com/sites/SPODemo/Test%20Upload/Forms" as the url to upload to but I continually get a 401 Unauthorized error. The logon id I'm using is the one I use to log on to our company network, which has been give the correct privileges to create files on the aspx page (I can upload files to the SP site via a browser).

I've tried variations of the code below but always get a 401 error:

private void WriteFileToSharepoint(string fileName)
    string siteUrl = "https://myorg.sharepoint.com/sites/SPODemo";
    var login = "[email protected]"; 
    var password = "my_windows_pw";
    var documentLibrary = "Test%20Upload"; 
    //var folderUrl = "/sites/SPODemo/" + documentLibrary + "/Forms";
    var folderUrl = "/sites/SPODemo/" + documentLibrary;

        using (var stream = new MemoryStream())
            FileStream fileStream = System.IO.File.OpenRead(fileName);
            string source_filename = Path.GetFileName(fileName);
            FileCreationInformation fci = new FileCreationInformation
                Overwrite = false,
                Url = source_filename,
                ContentStream = fileStream

            using (var ctx = new ClientContext(siteUrl))
                var securePassword = new SecureString();
                foreach (char c in password.ToCharArray()) securePassword.AppendChar(c);
                NetworkCredential _myCredentials = new NetworkCredential(login, password, "myorg");
                ctx.Credentials = _myCredentials;
                var targetFileUrl = String.Format("{0}/{1}", folderUrl, fci.Url);
                Microsoft.SharePoint.Client.File.SaveBinaryDirect(ctx, targetFileUrl, fci.ContentStream, true);

            // Also tried ctx.Load(targetFileUrl) then ctx.ExecuteQuery();
    catch (Exception ex)

The error messages I get:

  • Cannot contact web site 'https://myorg.sharepoint.com/' or the web site does not support SharePoint Online credentials. The response status code is 'Unauthorized'.
  • Access denied. Before opening files in this location you must first browse to the web site and select the option to login automatically.

Thanks in advance.

1 Answer 1


I've also faced this same problem and according to this site you cannot use NTML password anymore when connect to SharePoint Online using ClientContext. You must use App Authentication.

This is how you can use that authentication.

First you need to register you app.

  1. Head to your site and enter this address _layouts/15/appregnew.aspx (ex: https://myorg.sharepoint.com/sites/something/_layouts/15/appregnew.aspx)
  2. Generate ClientID and Client Secret and save them somewhere.
  3. Click on Create button
  4. Now head to https://xxxx-admin.sharepoint.com/_layouts/15/appinv.aspx, xxxx is the name of your site (myorg)
  5. Insert ClientID to App Id field and click on Lookup
  6. The fields will be filled with the information you provided when you register the app
  7. Insert this xml into fields Permission Request XML App Permission
  8. Click on Create and then Trust It

You need to use PnP framework from NuGet (mine is PnP.Framework, Version= This is how you get ClientContext in your program

private ClientContext GetClientContext => new PnP.Framework.AuthenticationManager().GetACSAppOnlyContext(siteUrl, clientId, clientSecret);

PnP framework only available in .net standard 2 and not .net framework. So maybe you can just create a new .net standard project and add reference to it from your project (according to this sites Using .NET standard with Full Framework .NET). You can then run PnP framework from your .net standard.

This is the App Permission:

<AppPermissionRequests AllowAppOnlyPolicy="true">
  <AppPermissionRequest Scope="http://sharepoint/content/tenant" Right="FullControl" />

This are AppPermissionRequest right values (see here for full explanation):

  • Read
  • Write
  • Manage
  • FullControl

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.