如何通过sharepoint匿名下载文件没有登录C#中的必需链接? [英] How to donwload a file anonymously via a sharepoint no sign in required link in C#?
问题描述
我可以通过sharepoint下载文件,在提供用户和密码后无需在C#中签名所需的链接,但如何在不提供用户和密码的情况下下载文件?
I can download a file via a sharepoint no sign in required link in C# after giving user and password, but how to download a file without provding user and password?
我的步骤
1。通过选择"查看链接 - 无需登录",从sharepoint中的文件中获取链接(fileUrl)。
1. Get a link ( fileUrl ) from a file in sharepoint by selecting "View link - no sign-in required".
2。我的代码:
Microsoft.SharePoint.Client.ClientContext context = new Microsoft.SharePoint.Client.ClientContext(sharepointRootUrl);
Microsoft.SharePoint.Client.ClientContext context = new Microsoft.SharePoint.Client.ClientContext( sharepointRootUrl );
SecureString passWord = new SecureString();
SecureString passWord = new SecureString();
string pw =" xxxxxxx";
string pw = "xxxxxxx";
foreach(var c in pw.ToCharArray())passWord.AppendChar(c );
foreach (var c in pw.ToCharArray()) passWord.AppendChar(c);
string user =" xxxxxxxxx";
string user = "xxxxxxxxx";
context.Credentials = new SharePointOnlineCredentials(user,passWord);
context.Credentials = new SharePointOnlineCredentials(user, passWord);
Microsoft.SharePoint.Client.File file = context.Web.GetFileByGuestUrl(fileUrl);
Microsoft.SharePoint.Client.File file = context.Web.GetFileByGuestUrl( fileUrl );
...
推荐答案
您好,
以下是要下载的代码段来自Guest Link的文件供您参考:
Here is the code snippet to download file from Guest Link for your reference:
string userName = "xxx@xxx.onmicrosoft.com";
string password = "xxx";
var securePassword = new SecureString();
foreach (char c in password)
{
securePassword.AppendChar(c);
}
using (var clientContext = new ClientContext("https://xxx.sharepoint.com/sites/xxx"))
{
clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);
Web web = clientContext.Web;
clientContext.Load(web, a => a.ServerRelativeUrl);
clientContext.ExecuteQuery();
Microsoft.SharePoint.Client.File file = clientContext.Web.GetFileByGuestUrl("https://xxx.sharepoint.com/:x:/s/xxx/ESRjbSijEHhEvBiaiSDKVM0Bu9xFVhzxvwRrOby1--ROaQ");
clientContext.Load(file);
clientContext.ExecuteQuery();
if (file != null)
{
FileInformation fileInfo = File.OpenBinaryDirect(clientContext, file.ServerRelativeUrl);
clientContext.ExecuteQuery();
var folderpath = @"c:\downloadfolder";
if (!System.IO.Directory.Exists(folderpath))
{
System.IO.Directory.CreateDirectory(folderpath);
}
using (var fileStream = new System.IO.FileStream(folderpath + @"\" + file.Name, System.IO.FileMode.Create))
{
fileInfo.Stream.CopyTo(fileStream);
}
}
}
谢谢
最好的问候
这篇关于如何通过sharepoint匿名下载文件没有登录C#中的必需链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!