How to perform SharePoint Online authentication in console APP using CSOM

Lakshman Bana 61 Reputation points
2021-04-01T17:29:19.64+00:00

Hello,

I'm trying to connect to myfuturenet.sharepoint.com in .Net console application to add/remove elements to the list item. In this process, I'm using ClientContext but most of the examples are using Userid and Password in SharePointOnlineCredentials(). My objective is not to ask User ID and Password in the application but to use existing authentication credentials that were generated with SSO.

Any pointers on this would be a great help. Thanks in advance.

lb

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,621 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jerryzy 10,566 Reputation points
    2021-04-02T01:55:54.977+00:00

    Hi @Lakshman Bana ,

    PnP.Framework Library supported to use App Id and App Secret to get the ClientContext and this library supported both .NET Framework and .NET Standard:

    Please first install PnP.Core as this is the dependent package for PnP.Framework:

    Install-Package PnP.Core -Version 1.1.0  
    Install-Package PnP.Framework -Version 1.4.0  
    

    Then authentication with App Id and AppSecret ( register in https://siteurl/_layouts/15/AppRegNew.aspx) like this:

    using Microsoft.SharePoint.Client;  
    using PnP.Framework;  
      
    ClientContext ctx = new AuthenticationManager().GetACSAppOnlyContext("SiteUrl", "AppId", "AppSecret");  
    ctx.Load(ctx.Web);  
    ctx.ExecuteQuery();  
    

    Here is a detailed blog for your reference:

    M365 – SharePoint Online – CSOM – Getting SharePoint client context using PnP.Framework in .NET Core application

    Thanks
    Best Regards


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    4 people found this answer helpful.

5 additional answers

Sort by: Most helpful
  1. Trevor Seward 11,681 Reputation points
    2021-04-01T17:47:05.84+00:00

    The cookies aren't shared between your browser and your application, thus you will need to either authenticate interactively or use Client ID + Secret/Certificate for unattended authentication.

    1 person found this answer helpful.
    0 comments No comments

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. gsaunders 96 Reputation points
    2021-04-01T19:29:42.45+00:00

    Here is link to where I had issue getting .NET Standard (5.0) working with app permissions using client / secret and ended up getting things working with client / certificate.

    Again... not SSO, but may help someone dealing with app based permission.

    webexception-in-microsoft-sharepoint-client-runtime-dll.6756

    0 comments No comments

  4. Lakshman Bana 61 Reputation points
    2021-04-02T11:49:39.81+00:00

    Thanks for all the pointers. I think they look promising and I'm going to try it out today. I'll post the results once I make some progress.
    Thanks once again for the pointers.
    lb