在没有密码的情况下将文件上传到共享点 [英] Uploading a file to sharepoint without a password

查看:75
本文介绍了在没有密码的情况下将文件上传到共享点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 microsoft sharepoint 客户端使用 c# 将文件上传到 sharepoint

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();

                }

但我的问题出现在网络凭据部分.有没有办法使用当前用户的凭据(通过 iis 或 .net 或任何东西),这样我就不必询问他们的密码?因为这不是我们想以纯文本形式保存在任何地方的内容.

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.

先谢谢你

推荐答案

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

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.

这篇关于在没有密码的情况下将文件上传到共享点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆