SPO Tenant authientication with MFA configured account CSOM application

Shri-6058 326 Reputation points
2021-09-23T20:50:53.79+00:00

Hi I have the working tool for the UPS access and updates. Because I didnt have SharePoint administrator role, I was unable to perform actual property updates. After I received SP administrator role(with MFA - this is our company policy elivated permission should have a MFA configured), The following authCookie (last line) fails staing Miss match with the stored Microsoft account details in the system.. This is because we have MFA configured. code has to be entered. I then did PnP.Framework setup however I am not able to replace the following with the PnP MFA authentication code replacement. Will you advise?

static bool InitializeWebService()
{
try
{
string webServiceExt = "_vti_bin/userprofileservice.asmx";
string adminWebServiceUrl = string.Empty;
//append the web service (ASMX) url onto the admin web site URL
if (_profileSiteUrl.EndsWith("/"))
adminWebServiceUrl = _profileSiteUrl + webServiceExt;
else
adminWebServiceUrl = _profileSiteUrl + "/" + webServiceExt;
LogMessage("Initializing SPO web service " + adminWebServiceUrl, LogLevel.Information);
SecureString securePassword = GetSecurePassword(_sPoAuthPasword);
SharePointOnlineCredentials onlineCred = new SharePointOnlineCredentials(_sPoAuthUserName, securePassword);

            // Get the authentication cookie by passing the url of the admin web site 
            string authCookie = **onlineCred.GetAuthenticationCookie(new Uri(_profileSiteUrl));**

// Setting up the user profile web service
_userProfileService = new UPSvc.UserProfileService();

            // assign the correct url to the admin profile web service 
            _userProfileService.Url = adminWebServiceUrl;

            // Assign previously created auth container to admin profile web service 
            _userProfileService.CookieContainer = authContainer;
            // LogMessage("Finished creating service object for SPO Web Service " + adminWebServiceUrl, LogLevel.Information);
            return true;

Please let me know. Appreciate any help.

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,615 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,573 questions
SharePoint Server Management
SharePoint Server Management
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Management: The act or process of organizing, handling, directing or controlling something.
2,798 questions
{count} votes

12 answers

Sort by: Most helpful
  1. MichaelHan-MSFT 18,016 Reputation points
    2021-09-30T09:58:19.79+00:00

    Hi @Shri-6058 ,

    Which site did you connect? Please make sure you are connecting to the admin center site url:

    var siteUrl = "https://contoso-admin.sharepoint.com/";  
    
    1 person found this answer helpful.
    0 comments No comments

  2. MichaelHan-MSFT 18,016 Reputation points
    2021-09-24T07:08:30.1+00:00

    Hi @Shri-6058 ,

    The SharePointOnlineCredentials class would not work for tenant with MFA configured.

    For your requirement, I would suggest you use CSOM to read or update user profile properties: https://learn.microsoft.com/en-us/sharepoint/dev/solution-guidance/read-or-update-user-profile-properties-sample-app-for-sharepoint


    If an Answer 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.

    0 comments No comments

  3. Shri-6058 326 Reputation points
    2021-09-24T07:42:36.917+00:00

    Thank you MichaelHan. I have already overcome the issue with SharePointOnlineCredentioals using PnP framework already. That part is working just fine using below code.

                string siteUrl = "https://MyCompany-admin.sharepoint.com";
                var authManager = new OfficeDevPnP.Core.AuthenticationManager();
                ClientContext ctx = authManager.GetWebLoginClientContext(siteUrl);
                Web web = ctx.Web;
                ctx.Load(web, w => w.Title);
                ctx.ExecuteQuery();
    

    I only have issues now with just updating the properties last function "_userProfileService.ModifyUserPropertyByAccountName(UserName, data);" It says "attempted to perform unauthorized operation". I have sharepoint administrator role, I verified UPS server has full permission to my account still its throwing that error made me stuck.

    Please let me know if you have any thoughts.


  4. Shri-6058 326 Reputation points
    2021-09-24T17:37:08.267+00:00

    @MichaelHan-MSFT The link you provided is no longer available. 404.

    0 comments No comments

  5. Shri-6058 326 Reputation points
    2021-09-24T18:12:06.577+00:00

    Unfortunately, this error was from 2014. The following blog is exactly what I am experiencing. As soon as I tried to modify the properties for others, it ends up with unauthorized operations"

    http://jopx.blogspot.com/2014/01/automatically-updating-user-profile.html

    Anyone experienced this? Please let me know how to overcome updating properties of everyone. I am a SharePoint Administrator, Global administrator still not able to execute "ModifyUserPropertyByAccountName(UserName, data)"

    0 comments No comments