What is the alternative of GetWebLoginClientContext in the current PnP.Framework?

Jack Le 65 Reputation points
2023-02-03T01:04:49.1433333+00:00

Following this issue, I have copied the code for GetWebLoginClientContext and implemented it in my project. However, I get this out-of-date browser error because the code uses System.Windows.Forms.WebBrowser which uses the old built-in browser.

Capture

What are the alternative ways of implementing this? My client has MFA turned on so the application needs to have this Web Login to go through the MFA steps.

Developer technologies | Windows Forms
Microsoft 365 and Office | SharePoint | Development
Developer technologies | .NET | Other
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} votes

Answer accepted by question author
  1. RaytheonXie_MSFT 40,496 Reputation points Microsoft External Staff
    2023-02-28T02:27:17.4433333+00:00

    Hi @Jack Le

    You can try to use older version of PnP.Framework since the latest version has delete the GetWebLoginClientContext function.

    Please refer to the steps

    1. Go to Manage NuGet Packet.
    2. choose older version such as 3.25 User's image
    3. Now the authManager.GetWebLoginClientContext(siteUrl) is available

1 additional answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,496 Reputation points Microsoft External Staff
    2023-02-03T07:09:10.4533333+00:00

    Hi @Jack Le

    Per my research, AuthenticationManager has depatched GetWebLoginClientContext. I will recommend you to use SharePoint app only to connect SharePoint Online with MFA. We need to connect SharePoint without using the PnP Framework library. So you can refer to the following code

    using Microsoft.SharePoint.Client;
    using System;
    
    namespace AzureACSAuth
    {
        class Program
        {
            static void Main(string[] args)
            {
                string siteUrl = "https://contoso.sharepoint.com/sites/demo";
    
                //Get the realm for the URL
                string realm = TokenHelper.GetRealmFromTargetUrl(new Uri(siteUrl));
    
                //Get the access token for the URL.  
                string accessToken = TokenHelper.GetAppOnlyAccessToken(TokenHelper.SharePointPrincipal, new Uri(siteUrl).Authority, realm).AccessToken;
    
                //Create a client context object based on the retrieved access token
                using (ClientContext cc = TokenHelper.GetClientContextWithAccessToken(siteUrl, accessToken))
                {
                    cc.Load(cc.Web, p => p.Title);
                    cc.ExecuteQuery();
                    Console.WriteLine(cc.Web.Title);
                }
            }
        }
    }
    
    

    And the app.config should be like following

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings>
        <!-- Use AppRegNew.aspx and AppInv.aspx to register client id with secret -->
        <add key="ClientId" value="[Your Client ID]" />
        <add key="ClientSecret" value="[Your Client Secret]" />
      </appSettings>
    </configuration>
    
    

    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.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.