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

Jack Le 45 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.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,903 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,924 questions
C#
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.
11,016 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,044 questions
0 comments No comments
{count} votes

Accepted answer
  1. RaytheonXie_MSFT 36,171 Reputation points Microsoft Vendor
    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 36,171 Reputation points Microsoft Vendor
    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 Answers by the question author, which helps users to know the answer solved the author's problem.