C# CSOM不使用windows凭据并提供;远程服务器返回错误:(403)禁止。”;错误

C# CSOM不使用windows凭据并提供;远程服务器返回错误:(403)禁止。”;错误,c#,sharepoint,csom,C#,Sharepoint,Csom,我有一个带有.NET Framework的控制台应用程序,它使用CSOM上传文件。当我运行应用程序时,出现以下错误: 远程服务器返回错误:(403)禁止 但是,当我通过windows凭据时,它工作正常。出于安全目的,我不想在代码中硬编码我的凭证。除了在代码中传递凭证外,还有其他解决方法吗 我的代码如下: using System; using System.IO; using System.Linq; using System.Security; using Microsoft.SharePoi

我有一个带有.NET Framework的控制台应用程序,它使用CSOM上传文件。当我运行应用程序时,出现以下错误:

远程服务器返回错误:(403)禁止

但是,当我通过windows凭据时,它工作正常。出于安全目的,我不想在代码中硬编码我的凭证。除了在代码中传递凭证外,还有其他解决方法吗

我的代码如下:

using System;
using System.IO;
using System.Linq;
using System.Security;
using Microsoft.SharePoint.Client;
using ClientOM = Microsoft.SharePoint.Client;


namespace ConsoleApp3
{

class Program
{
    static void Main(string[] args)
    {

        ClientContext clientContext = new ClientContext("https://companyname.sharepoint.com");

        //SecureString passWord = new SecureString();
        //foreach (char c in "HelloWorld@1234".ToCharArray()) passWord.AppendChar(c);
        //clientContext.Credentials = new SharePointOnlineCredentials("jay.desai@company.com", passWord);
        clientContext.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

        using(FileStream fileStream = new FileStream(@"C:\Users\jay.desai\Desktop\LSRSQL01_ACXM_20201003.html", FileMode.Open))
            ClientOM.File.SaveBinaryDirect(clientContext, "/sites/DataServices/Shared Documents/Data Dictionaries/LSRSQL01_ACXM_20201003.html", fileStream, true);
    }
}

}

CSOM connect SharePoint Online需要SharePointOnline凭据。 测试演示:

static void Main(string[] args)   
      {  
            string userName = "amos@contoso.onmicrosoft.com";  
            Console.WriteLine("Enter your password.");  
            SecureString password = GetPassword();                  
            using(var clientContext = new ClientContext("https://contoso.sharepoint.com"))   
            {  
                // SharePoint Online Credentials  
                clientContext.Credentials = new SharePointOnlineCredentials(userName, password);  
                // Get the SharePoint web  
                Web web = clientContext.Web;  
                // Load the Web properties  
                clientContext.Load(web);  
                // Execute the query to the server.  
                clientContext.ExecuteQuery();  
                // Web properties - Display the Title and URL for the web  
                Console.WriteLine("Title: " + web.Title + "; URL: " + web.Url);  
                Console.ReadLine();  
                }  
            } 

private static SecureString GetPassword()  
      {  
            ConsoleKeyInfo info;  
            //Get the user's password as a SecureString  
            SecureString securePassword = new SecureString();  
            do   
            {  
                info = Console.ReadKey(true);  
                if (info.Key != ConsoleKey.Enter)   
                {  
                    securePassword.AppendChar(info.KeyChar);  
                }  
            }  
            while (info.Key != ConsoleKey.Enter);  
            return securePassword;  
        } 
资料来源:

更新:

string siteUrl = "https://contoso.sharepoint.com/sites/demo";
using (var cc = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, "[Your Client ID]", "[Your Client Secret]"))
{
    cc.Load(cc.Web, p => p.Title);
    cc.ExecuteQuery();
    Console.WriteLine(cc.Web.Title);
};

使用默认凭据,用户在本地和远程计算机上都需要相同的Windows帐户。所以这两台机器需要在同一个组(或组策略)中,所以这两台机器都使用相同的密码服务器作为用户帐户。@jdweng,是的,这两台机器都有相同的密码和用户帐户。我使用相同的用户名和密码登录到本地系统以及share point或office 365。这不是我要求的。这两台机器需要连接起来。拥有相同的用户名和密码并不意味着它是同一个帐户。与其他注释一样,提到机器和目标需要在同一个AD中,也可能存在用于身份验证的MFA步骤。除了
clientContext.Credentials=System.Net.CredentialCache.DefaultNetworkCredentials,还有其他选择吗
除了硬编码用户名和密码之外?您可以硬编码密码。var pwd=“mypassword”;var passWord=new SecureString();foreach(pwd.ToCharArray()中的字符c)passWord.AppendChar(c);ctx.Credentials=新的SharePointOnlineCredentials(“admin@mydomain.com“,密码);我已经这么做了。如果您看到我的代码,我已经对其进行了注释。我不想硬编码,这就是将此帖子放在堆栈溢出上的全部意义。嗯,您可以使用SharePoint应用程序访问SharePoint Online。正式文档: