0

I have created an azure function in Visual studio which is used to check if the logged in user has a permission to SharePoint, my problem is when the user has MFA enabled I get message that "The user name and password does not match with the on in Microsoft Account" So I wrote another function using SharePoint PNP core online, which was suggested in such case or MFA enabled users, so now when I run the code in debug mode my local host works fine. But when I check with the public URL is get 502 error. Can anyone suggest me?

My Azure function to check for SPO credentials with MFA enabled.

private static bool isValidUserAfterMFA(string siteURL, string userName, string Password)
        {
            bool checkMFA = false;
            try
            {

                var authManager = new OfficeDevPnP.Core.AuthenticationManager();
                // This method calls a pop up window with the login page and it also prompts 
                // for the multi factor authentication code. 
                ClientContext ctx = authManager.GetWebLoginClientContext(siteURL);
                // The obtained ClientContext object can be used to connect to the SharePoint  
                Web web = ctx.Web;
                ctx.Load(web);
                ctx.ExecuteQuery();
                checkMFA = true;
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
            return checkMFA;
        }

if (isValidUser(siteCollectionUrl, userName, password))
            {

                name = "Success";
            }
            else if (isValidUserAfterMFA(siteCollectionUrl, userName, password))
            {
                name = "SuccessafterMFA";
            }
            else
            {
                name = "Fail";
            }

return name == null
            ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query 
string or in the request body")
            : req.CreateResponse(HttpStatusCode.OK, name);

My Front end which triggers Azure function Below.

public async ValidateESignature(e) {
debugger;

var self = this;
this.setState({
  disableSubmit: true,
});
//self.setState({ ResponseModelOpen: true });
return await $.ajax({
  url:
    "https://sapauthpoc.azurewebsites.net/api/myfunction?name=" +
    this.props.currentUserEmail +
    "|" +
    this.state.Password,
  type: "GET",
  crossDomain: true,
  headers: {
    accept: "application/json;odata=verbose"
    
  },
  success: async function (data) {
    console.log(data);
    //alert(data);
    if (data === "Success") {
      debugger;

      self.saveData();

      //
    } else if(data == "SuccessafterMFA"){
      debugger;
      self.saveData();
    }else {
      self.setState({
        AuthenticationMsg: "Authentication failed",
        showLoader: false,
        disableSubmit: false,
      });

      setTimeout(() => {
        self.setState({ AuthenticationMsg: "", Password: "" });
      }, 3000);
    }
  },
  error: function (data) {
    console.log(data);
  },
});
e.preventDefault();

}

The above code works fine when I run this on Localhost api Eg: Works - http://localhost:7071/api/myfunction?name=myname.domain.com|xyz@123456 Fails - https://sapauthpoc.azurewebsites.net/api/myfunction?name=myname.domain.com|xyz@123456 gives 502 Bad error.

Any Help will be appreciated

1 Answer 1

1

If MFA is enabled for account, you will only be able to access the resources through Interactive login.

MFA enabled account can not be use to run application in background or to run daemon app.

In order to access the SharePoint online resource in Azure Function you will require an app registration and pass that app registration details like Client ID, Tenant ID, Thumbprint in Azure Function.

For App registration use below PowerShell script.

For details steps for accessing SharePoint in Azure function refer the below docs.

1
  • Hi thank you for your response, but is there a way to do it with emai and password?
    – Ninad J
    Commented Sep 19, 2022 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.