Hi @Amit Singh Rawat ,
Using class SharePointOnlineCredentials
with userName and password to authenticate in SharePoint does not support for MFA enviroment.
As a workaround, you could install the package SharePointPnPCoreOnline: https://www.nuget.org/packages/SharePointPnPCoreOnline. And use the below way to get clientcontex.
string siteUrl = "https://<tenant-name>.sharepoint.com/sites/test";
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);
Another workaround is using App Only autentication: https://www.c-sharpcorner.com/article/connect-to-sharepoint-online-site-with-app-only-authentication/
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.