Windows Authentication in Sharepoint Online/365 using CSOM in C++

NPE 1 Reputation point
2022-10-04T01:32:10.993+00:00

Hi, I am trying to develop an app that will do some stuffs in SharePoint Online (authenticate, upload, download, etc..).

I am able to do all this stuffs fine by authenticating by passing a given username and password using SharePointOnlineCredentials(usrname, SecurePass) for context credentials.

Something like:

usrname = "abcd";  
SecureString^ SecurePass = "efg"; //sample only  

Microsoft::SharePoint::Client::ClientContext^ ctx = gcnew Microsoft::SharePoint::Client::ClientContext(spUrl);  
ctx->Credentials = gcnew SharePointOnlineCredentials(usrname, SecurePass);  

This works as long as I supply the username and password but is there a way to authenticate using the current windows credentials (assuming that I have an account in SharePoint online)? Like some code to use the current user windows credentials without me supplying the username and password? Is it possible in Sharepoint online?

I read some articles that mentions "X-FORMS_BASED_AUTH_ACCEPTED" but there isn't a lot of samples about it for SharePoint Online.

Any help would be appreciated. Thanks!

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,553 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,516 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Tong Zhang_MSFT 9,116 Reputation points
    2022-10-04T06:51:13.12+00:00

    Hi @NPE ,

    According to my research and testing, you can use App Only Authentication, first you need to register the app in the SharePoint, grant proper permission, and then use the following code via CSOM:

    string siteUrl = "https://contoso.sharepoint.com/sites/demo";  
    using (var cc = new AuthenticationManager().GetACSAppOnlyContext(siteUrl, "[Your Client ID]", "[Your Client Secret]"))  
    {  
        cc.Load(cc.Web, p => p.Title);  
        cc.ExecuteQuery();  
        Console.WriteLine(cc.Web.Title);  
    };  
    

    You can follow the steps in this document: Granting access using SharePoint App-Only

    Also, our forum mainly discuss about SharePoint development using C#, as your issue is about development using C++, we suggest you can create a new thread on techcommunity for help:

    techcommunity :

    https://techcommunity.microsoft.com/t5/sharepoint-developer/bd-p/SharePointDev

    Hope it can help you. Thanks for your understanding.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    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.