Hi @lieee01 ,
The windows credentials(DefaultCredentials) is not supported to access SharePoint Online site.
You need to use SharePointOnlineCredentials for authentication, the username and password is the Microsoft 365 accounts of your organization to access SharePoint Online. Below is a sample to access SharePoint Online:
String name = "userName@tenant.onmicrosoft.com";
String password = "xxxx";
SecureString securePassword = new SecureString();
foreach (char c in password.ToCharArray())
{
securePassword.AppendChar(c);
}
var credentials = new SharePointOnlineCredentials(name, securePassword);
var siteUrl = "https://yourSite.sharepoint.com/sites/test";
ClientContext ctx = new ClientContext(siteUrl);
Web web = ctx.Web;
ctx.Load(web);
ctx.ExecuteQuery();
Besides, you could use app only with client Id and client secret to access SharePoint Online, this requires you register an app and grant permissions for the app. You could refer to this official article for more: https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs
If an Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.