如何解决Office 365连接错误和SharePoint Online凭据
目前,我正在尝试通过我的应用程序连接到Office 365 SharePoint,并使用以下方法,以便在连接列表,文档上传等连接后可以执行进一步
static string siteUrl = "https://<<Site URL>>";
static string userName = "xxxxxxxx";
static string password = "xxxxxxxxx";
public async static void Uploadfiles()
{
try
{
ClientContext ctx = new ClientContext(siteUrl);
SecureString secureString = new SecureString();
password.ToList().ForEach(secureString.AppendChar);
ctx.Credentials = new SharePointOnlineCredentials(userName, secureString);
Web web = ctx.Web;
Site site = ctx.Site;
ctx.Load(site);
ctx.ExecuteQuery();
Console.WriteLine(site.Url.ToString());
}
catch (Exception ex)
{
System.Console.WriteLine("Exception occurred : " + ex.Message);
System.Console.ReadLine();
}
}
操作
Cannot contact web site 'https://<<Site Url>>/' or the web site does not support SharePoint Online credentials. The response status code is 'Unauthorized'. The response headers are 'X-SharePointHealthScore=1, X-MSDAVEXT_Error=917656; Access+denied.+Before+opening+files+in+this+location%2c+you+must+first+browse+to+the+web+site+and+select+the+option+to+login+automatically., SPRequestGuid=0e6f41a0-90f3-1000-2182-78f5074383d4, request-id=0e6f41a0-90f3-1000-2182-78f5074383d4, MS-CV=oEFvDvOQABAhgnj1B0OD1A.0, Strict-Transport-Security=max-age=31536000, SPRequestDuration=15, SPIisLatency=0, MicrosoftSharePointTeamServices=16.0.0.22504, X-Content-Type-Options=nosniff, X-MS-InvokeApp=1; RequireReadOnly, X-Cache=CONFIG_NOCACHE, X-MSEdge-Ref=Ref A: 45D6DA1299E1407E9FD61C019494216D Ref B: BL2EDGE2116 Ref C: 2022-05-27T09:06:08Z, Content-Length=0, Content-Type=text/plain; charset=utf-8, Date=Fri, 27 May 2022 09:06:08 GMT, P3P=CP="ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI", X-Powered-By=ASP.NET'.
的 站点层面的任何东西,否则我的方法都缺少任何东西。
Currently i am trying to connect to office 365 sharepoint via my my application and used below approach so i can perform further operations after connecting like listing, document upload etc.
static string siteUrl = "https://<<Site URL>>";
static string userName = "xxxxxxxx";
static string password = "xxxxxxxxx";
public async static void Uploadfiles()
{
try
{
ClientContext ctx = new ClientContext(siteUrl);
SecureString secureString = new SecureString();
password.ToList().ForEach(secureString.AppendChar);
ctx.Credentials = new SharePointOnlineCredentials(userName, secureString);
Web web = ctx.Web;
Site site = ctx.Site;
ctx.Load(site);
ctx.ExecuteQuery();
Console.WriteLine(site.Url.ToString());
}
catch (Exception ex)
{
System.Console.WriteLine("Exception occurred : " + ex.Message);
System.Console.ReadLine();
}
}
But when it executes query it throws below error:
Cannot contact web site 'https://<<Site Url>>/' or the web site does not support SharePoint Online credentials. The response status code is 'Unauthorized'. The response headers are 'X-SharePointHealthScore=1, X-MSDAVEXT_Error=917656; Access+denied.+Before+opening+files+in+this+location%2c+you+must+first+browse+to+the+web+site+and+select+the+option+to+login+automatically., SPRequestGuid=0e6f41a0-90f3-1000-2182-78f5074383d4, request-id=0e6f41a0-90f3-1000-2182-78f5074383d4, MS-CV=oEFvDvOQABAhgnj1B0OD1A.0, Strict-Transport-Security=max-age=31536000, SPRequestDuration=15, SPIisLatency=0, MicrosoftSharePointTeamServices=16.0.0.22504, X-Content-Type-Options=nosniff, X-MS-InvokeApp=1; RequireReadOnly, X-Cache=CONFIG_NOCACHE, X-MSEdge-Ref=Ref A: 45D6DA1299E1407E9FD61C019494216D Ref B: BL2EDGE2116 Ref C: 2022-05-27T09:06:08Z, Content-Length=0, Content-Type=text/plain; charset=utf-8, Date=Fri, 27 May 2022 09:06:08 GMT, P3P=CP="ALL IND DSP COR ADM CONo CUR CUSo IVAo IVDo PSA PSD TAI TELo OUR SAMo CNT COM INT NAV ONL PHY PRE PUR UNI", X-Powered-By=ASP.NET'.
Any idea do i need to configure anything at site level or i am missing anything in my approach.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SharePointonLinlineCredentials使用遗留身份验证,默认情况下,这些验证已被禁用在租户上。如果您有租户管理权,则可以在不关心安全问题的情况下启用它。
否则,您需要使用OAuth代币身份验证,查看MSAL或PNP Framework库(Authentication Manager类)。
SharepointOnlineCredentials uses legacy authentication that is now by default disabled on the tenants. If you have tenant admin rights, you could enable it if you do not care about the security issue.
Otherwise, you need to use OAUTH token authentication, take a look at the MSAL or PNP Framework libraries (Authentication Manager class).