Upload file to SharePoint using Microsoft credentials in .NET
I am creating a custom web app (in ASP.NET MVC) that uploads files to a SharePoint Site. My app asks to Login using a Microsoft account.Once the user has logged in, it takes him to my custom app’s views. I want the user to be able to upload a file to a SharePoint site (by clicking a button in my app) and the app to upload the file using the user’s MS credentials (without asking the user for the email and password again). Is there any way of uploading files to a SharePoint site using the credentials of the logged in user? Is there a way of getting a security token, or similar, to do this?
This is how I’m connecting to a Library in SharePoint with the UserEmail and Password hardcoded:
string siteUrl = ConfigurationManager.AppSettings["SharepointUrl"]; string docLibraryName = ConfigurationManager.AppSettings["SharepointLibrary"]; string userName = ConfigurationManager.AppSettings["UserName"]; SecureString securedPassword = new SecureString(); foreach (var c in ConfigurationManager.AppSettings["Password"]) { securedPassword.AppendChar(c); } try { using (var context = new ClientContext(siteUrl)) { context.Credentials = new SharePointOnlineCredentials(userName, securedPassword); List docLib = context.Web.Lists.GetByTitle(docLibraryName); context.Load(docLib); context.ExecuteQuery(); } }
I’ve been doing some research about this and I found this two pages, but it didn’t work…