使用当前用户登录 SharePoint PnP AuthenticationManager

问题描述

我正在开发 .NET VSTO 插件,并希望使用 SharePoint PnP 库本地发布到 Sharepoint Online。我不清楚如何使用 AuthenticationManager 使用当前登录用户的 Azure AD 凭据获取 ClientContext(假设这是可能的)。

我已经向 Azure AD 注册了 VSTO 插件,并希望可以根据以下代码调用 AuthenticationManager 而不显式设置用户名和密码:

            using (ClientContext context = authenticationManager.GetContext(site,userPrincipal,userPasswordSecure))
            {
                ClientSidePage myPage = new ClientSidePage(context);
                myPage.Save("Foo.aspx");
                myPage.Publish();
            }

基本上我正在寻找一个只需要传递站点参数的重载方法。这避免了用户必须明确设置他们的用户名和密码。

谢谢和问候, 安德鲁

解决方法

请尝试GetAzureADAccessTokenAuthenticatedContext(String,String)方法。

private static string xxx(string siteURL,string accessToken)
{
    var authMgr = new OfficeDevPnP.Core.AuthenticationManager();
    using (var ctx = authMgr.GetAzureADAccessTokenAuthenticatedContext(siteURL,accessToken))
    {
        ......
    }
}

有关代表用户获取访问权限的详细信息,请参阅 here