Uploading a file to sharepoint without a password

时间:2015-04-29 00:29:21

标签: c# .net iis sharepoint sharepoint-2010

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 个答案:

答案 0 :(得分:0)

使用CredentialCache.DefaultCredentials(https://msdn.microsoft.com/en-us/library/system.net.credentialcache.defaultcredentials(v=vs.110).aspx)代替NetworkCredential对象。

这将使用用户安全上下文。