SOLVED

Connect to SharePoint Online from ADAL protected MVC web app with logged in user's identity

%3CLINGO-SUB%20id%3D%22lingo-sub-390822%22%20slang%3D%22en-US%22%3EConnect%20to%20SharePoint%20Online%20from%20ADAL%20protected%20MVC%20web%20app%20with%20logged%20in%20user's%20identity%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-390822%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWe%20are%20trying%20to%20surface%20the%20results%20of%20our%20SharePoint%20Online%20search%20in%20an%20ADAL%20protected%20MVC%20web%20app.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ELooking%20at%20the%20SharePoint%20Online%20PnP%20Core%20CSOM%20extensions%20(OfficeDevPnP.Core%20namespace)%2C%20it%20looks%20like%20the%20method%20AuthenticationManager.GetAzureADWebApplicationAuthenticatedContext(String%2C%20Func)%20would%20be%20the%20way%20to%20go.%20However%2C%20We've%20been%20unable%20to%20find%20any%20useful%20documentation%20on%20this%20method%20(or%20the%20SharePoint%20Online%20PnP%20package%20as%20a%20whole)%2C%20in%20particular%20on%20how%20to%20obtain%20access%20tokens%2C%20or%20any%20working%20examples%20that%20do%20not%20include%20hard%20coding%20usernames%20and%20passwords.%20All%20the%20resources%20we've%20been%20able%20to%20find%20on%20the%20subject%20are%20either%20incomplete%20or%20out%20of%20date.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDoes%20detailed%20documentation%20exist%20for%20this%20package%3F%20Or%20any%20working%20examples%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThanks.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EFailing%20that%2C%20is%20there%20a%20way%20to%20achieve%20this%20by%20some%20other%20means%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-391190%22%20slang%3D%22en-US%22%3ERe%3A%20Connect%20to%20SharePoint%20Online%20from%20ADAL%20protected%20MVC%20web%20app%20with%20logged%20in%20user's%20identity%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-391190%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F39836%22%20target%3D%22_blank%22%3E%40Beau%20Cameron%3C%2FA%3E%26nbsp%3BYep%2C%20that%20did%20it!%20Thanks%20for%20your%20reply.%3C%2FP%3E%3CP%3EActually%20it%20was%20the%26nbsp%3B%3CSPAN%20class%3D%22pl-smi%22%3EMicrosoftGraphHelper%3C%2FSPAN%3E%3CSPAN%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pl-en%22%3EGetAccessTokenForCurrentUser%20method%20where%20the%20magic%20was%20-%20we%20just%20weren't%20getting%20the%20right%20access%20tokens.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-390988%22%20slang%3D%22en-US%22%3ERe%3A%20Connect%20to%20SharePoint%20Online%20from%20ADAL%20protected%20MVC%20web%20app%20with%20logged%20in%20user's%20identity%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-390988%22%20slang%3D%22en-US%22%3E%3CP%3EDoes%20this%20sample%20code%20still%20work%3F%3CBR%20%2F%3E%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FSharePoint%2FPnP%2Fblob%2Fmaster%2FSamples%2FBusinessApps.O365ProjectsApp%2FBusinessApps.O365ProjectsApp.Infrastructure%2FSPORemoteActions.cs%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fgithub.com%2FSharePoint%2FPnP%2Fblob%2Fmaster%2FSamples%2FBusinessApps.O365ProjectsApp%2FBusinessApps.O365ProjectsApp.Infrastructure%2FSPORemoteActions.cs%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3EUsing%20Client%20ID%20and%20Client%20Secret%20of%20the%20application%20to%20get%20the%20context%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New Contributor

Hi,

 

We are trying to surface the results of our SharePoint Online search in an ADAL protected MVC web app.

 

Looking at the SharePoint Online PnP Core CSOM extensions (OfficeDevPnP.Core namespace), it looks like the method AuthenticationManager.GetAzureADWebApplicationAuthenticatedContext(String, Func) would be the way to go. However, We've been unable to find any useful documentation on this method (or the SharePoint Online PnP package as a whole), in particular on how to obtain access tokens, or any working examples that do not include hard coding usernames and passwords. All the resources we've been able to find on the subject are either incomplete or out of date.

 

Does detailed documentation exist for this package? Or any working examples?

 

Thanks.

 

Failing that, is there a way to achieve this by some other means?

2 Replies
Highlighted
Best Response confirmed by Heisenberg (New Contributor)
Solution

Does this sample code still work?

https://github.com/SharePoint/PnP/blob/master/Samples/BusinessApps.O365ProjectsApp/BusinessApps.O365...

Using Client ID and Client Secret of the application to get the context?

Highlighted

@Beau Cameron Yep, that did it! Thanks for your reply.

Actually it was the MicrosoftGraphHelper.GetAccessTokenForCurrentUser method where the magic was - we just weren't getting the right access tokens.