Uploading a file to sharepoint without a password

此生再无相见时 提交于 2021-01-29 08:01:42

问题


i am trying to upload a file to sharepoint with c# using the microsoft sharepoint client

i have no issues when i create my context and give it my username and password like this

                using (ClientContext ctx = new ClientContext(spSite))
                {

                    if (!System.IO.File.Exists(fileLocation))
                        throw new FileNotFoundException("File not found.", fileLocation);

                    var credentials = new NetworkCredential("username", "password");
                    ctx.Credentials = credentials;
                    Web web = ctx.Web;

                    ctx.Load(user);
                    ctx.ExecuteQuery();
                    String fileName = System.IO.Path.GetFileName(fileLocation);
                    FileStream fileStream = System.IO.File.OpenRead(fileLocation);

                    ctx.Load(web.Folders);
                    ctx.ExecuteQuery();
                    Folder spFolder = web.Folders.FirstOrDefault(x => x.Name.Equals(spListCleanName));

                    FileCreationInformation fci = new FileCreationInformation();
                    fci.Url = spSite + spLibraryName + file;



                    byte[] bytes = System.IO.File.ReadAllBytes(fileLocation);
                    fci.Content = bytes;

                    Microsoft.SharePoint.Client.File spFile = spFolder.Files.Add(fci);
                    spFile.ListItemAllFields.Update();
                    ctx.ExecuteQuery();

                }

but my issue comes in the network credentials part. is there a way to use the current users credentials (through iis or .net or anything) so that i don't have to ask for their password? since that isn't something we want to save in plain text anywhere.

thank you in advance


回答1:


Use the CredentialCache.DefaultCredentials (https://msdn.microsoft.com/en-us/library/system.net.credentialcache.defaultcredentials(v=vs.110).aspx) instead of NetworkCredential object.

This will use the users security context.



来源:https://stackoverflow.com/questions/29932295/uploading-a-file-to-sharepoint-without-a-password

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!