C# SharePoint Online 错误:远程服务器返回错误:(403) FORBIDDEN

问题描述 投票:0回答:2

我想通过单点登录将 SharePoint Online 与 .NET 上的当前用户连接。我不想在我的代码中指定用户名和密码。不幸的是,我在 ExecuteQuery() 上收到以下错误消息:

The remote server returned an error: (403) FORBIDDEN

我的代码:

string siteCollectionUrl = "https://xxx.sharepoint.com/teams/yyyy";
System.Net.ICredentials credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
SharePoint.ClientContext context = new SharePoint.ClientContext(siteCollectionUrl);
context.Credentials = credentials;
SharePoint.Web web = context.Web;
context.Load(web);
context.ExecuteQuery();
string tt = web.Title;

你有什么想法吗?

提前致谢

c# .net sharepoint sharepoint-online
2个回答
0
投票

您必须使用 SharePointOnlineCredentials 类进行身份验证。

像这样:

        String name = "[email protected]";
        String password = "xxxx";
        SecureString securePassword = new SecureString();
        foreach (char c in password.ToCharArray())
        {
            securePassword.AppendChar(c);
        }
        var credentials = new SharePointOnlineCredentials(name, securePassword);
        string siteCollectionUrl = "https://xxx.sharepoint.com/teams/yyyy";

        ClientContext ctx = new ClientContext(siteCollectionUrl );
        ctx.Credentials = credentials;

0
投票

就我而言,有些事情不太好。 如果我添加凭据,错误将更改为“登录名或密码与 Microsoft 帐户系统中的登录名或密码不匹配” - 但用户名和密码都是正确的。

谢谢 米克洛斯

© www.soinside.com 2019 - 2024. All rights reserved.