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