Microsoft Edge WebView2 Autentificación .NET SharePoint

Eduardo Arturo Garcia Rios 1 Reputation point
2022-08-08T14:06:05.657+00:00

I have a project with WebView2 and I have to access the SharePoint portal already authenticated. this is my code

///----------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.SharePoint.Client;
using System.Security;

namespace logsharepoint

{
public partial class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}

        private void Form1_Load(object sender, EventArgs e)  
    {  
        string siteCollectionUrl = "https://empresa.sharepoint.com/portal";  
        string userName = "10MEX50AKS@empresa.onmicrosoft.com";  
        string password = "10mex3530414B53X";  
        Form1 obj = new Form1();  
        try  
        {  
            obj.ConnectToSharePointOnline(siteCollectionUrl, userName, password);  
        }  

        catch (Exception ex)  
        {  
            string msg = ex.Message.ToString();  
        }  
        label1.Text = siteCollectionUrl;  
        webView.Source = new System.Uri(siteCollectionUrl, System.UriKind.Absolute);  
    }  

    public void ConnectToSharePointOnline(string siteCollUrl, string userName, string password)  

    {  
        //Namespace: It belongs to Microsoft.SharePoint.Client  
        ClientContext ctx = new ClientContext(siteCollUrl);  
        // Namespace: It belongs to System.Security  
        SecureString secureString = new SecureString();  
        password.ToList().ForEach(secureString.AppendChar);  
        // Namespace: It belongs to Microsoft.SharePoint.Client  
        ctx.Credentials = new SharePointOnlineCredentials(userName, secureString);  
        // Namespace: It belongs to Microsoft.SharePoint.Client  
        Site mySite = ctx.Site;  
        ctx.Load(mySite);  
        ctx.ExecuteQuery();  
    }  
}  

}
///----------------------------------------

but it does not work. sharepoint keeps asking me for authentication

229221-image.png

Microsoft Edge
Microsoft Edge
A Microsoft cross-platform web browser that provides privacy, learning, and accessibility tools.
2,361 questions
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,934 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. RaytheonXie_MSFT 36,251 Reputation points Microsoft Vendor
    2022-08-09T07:57:44.22+00:00

    Hi @Anonymous ,
    I will recommend you to use CSOM access sharepoint online by following code

    string strUserName="abc";  
    string strPassword="123";  
    SecureString ssPwd = new SecureString();  
    strPassword.ToList().ForEach(ssPwd.AppendChar);  
    ClientContext context = new ClientContext(siteurl);  
    SharePointOnlineCredentials credentials = new SharePointOnlineCredentials(strUserName, ssPwd);  
      
    context.Credentials = credentials;  
    // The SharePoint web at the URL.  
    Web web = context.Web;  
      
    // We want to retrieve the web's properties.  
    context.Load(web);  
      
    // Execute the query to the server.  
    context.ExecuteQuery();  
    

    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.


    0 comments No comments

  2. Eduardo Arturo Garcia Rios 1 Reputation point
    2022-08-09T15:05:30.43+00:00

    Hello, thank you very much for your help, I tried your code and the same thing happens.

            private void Form1_Load(object sender, EventArgs e)  
        {  
            string siteCollectionUrl = "https://empresa.sharepoint.com/portal";  
           string userName = "10MEX50AKS@empresa.onmicrosoft.com";  
           string password = "10mex3530414B53X";  
            try  
            {  
                string strUserName = userName;  
                string strPassword = password;  
                SecureString ssPwd = new SecureString();  
                strPassword.ToList().ForEach(ssPwd.AppendChar);  
                ClientContext context = new ClientContext(siteCollectionUrl);  
                SharePointOnlineCredentials credentials = new SharePointOnlineCredentials(strUserName, ssPwd);  
    
                context.Credentials = credentials;  
                // The SharePoint web at the URL.  
                Web web = context.Web;  
    
                // We want to retrieve the web's properties.  
                context.Load(web);  
    
                // Execute the query to the server.  
                context.ExecuteQuery();  
                webView.Source = new System.Uri(web.Url.ToString(), System.UriKind.Absolute);               
            }  
            catch (Exception ex)  
            {  
                string msg = ex.Message.ToString();  
    
            }  
        }  
    

    Maybe the Webview does not get the authentication or what is going on

            webView.Source = new System.Uri(web.Url.ToString(), System.UriKind.Absolute);      
    
    0 comments No comments

  3. Bruce (SqlWork.com) 67,016 Reputation points
    2022-08-09T15:55:42.38+00:00

    online Sharepoint is dropping support for basic authentication. you should switch to the msal library to get the bearer tokens.

    0 comments No comments

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.