SharePoint PnP AuthenticationManager login with current user

2.9k views Asked by At

I am developing .NET VSTO Addin, and would like to publish natively to Sharepoint Online using the SharePoint PnP library. It is not clear to me how to use the AuthenticationManager to get a ClientContext using the current logged in user's Azure AD credentials (assuming this is possible).

I have registered the VSTO Addin with Azure AD, and was hoping that the AuthenticationManager could be called without explictly setting the username and password, per the following code:

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

Basically I am looking for an overloaded method that only needs the site paramter to be passed. This avoids the user having to explicitly set their username and password.

Thanks and regards, Andrew

1

There are 1 answers

1
unknown On BEST ANSWER

Please try GetAzureADAccessTokenAuthenticatedContext(String, String) Method.

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

For more information about getting access on behalf of a user, see here.