【发布时间】:2021-11-18 11:52:22
【问题描述】:
我正在尝试在控制台应用程序中在线连接到 SharePoint 并打印网站的标题。 它给了我错误:“登录名或密码与 Microsoft 帐户系统中的一个不匹配。” 我已检查并确保用户名和密码 100% 正确。 我不知道还要检查什么 这是我的代码:
private static void SPCredentialsConnect()
{
const string SiteUrl = "https://tenant.sharepoint.com/sites/mysite";
const string pwd = "appPassword";
const string username = "username@tenant.onmicrosoft.com";
SecureString securestring = new SecureString();
pwd.ToCharArray().ToList().ForEach(s => securestring.AppendChar(s));
ClientContext context = new ClientContext(SiteUrl);
context.Credentials = new SharePointOnlineCredentials(username, securestring);
try
{
var web = context.Web;
context.Load(web);
context.ExecuteQuery();
Console.WriteLine($"web title: {web.Title}");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
【问题讨论】:
-
更新:我已经尝试过 AppOnly 身份验证,但也没有用: public static void AppOnlyAuthCall() { using (var clientContext = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, clientId, clientSecret)) { clientContext .Load(clientContext.Web, page => page.Title); clientContext.ExecuteQuery(); Console.WriteLine(clientContext.Web.Title); }; }
-
当我使用此代码时,它告诉我“System.Net.WebException:'远程服务器返回错误:(401)未经授权。”是的,在 Azure Active Directory 中,我已授予 API 权限(委托 allsites 完全控制),“allowPublicClient”:true,“oauth2AllowUrlPathMatching”:true。
标签: c# authentication sharepoint console-application