C# 使用MFA通过C访问Sharepoint online#
我想使用非交互式C#程序从Sharepoint online下载一个文件。 最近启用了MFA,从那以后,我无法通过我的代码与任何用户一起获取文件,尽管我仍然可以通过门户网站访问它 首先,我尝试使用以下方法,获取的登录名或密码与Microsoft帐户系统中的登录名或密码不匹配。执行查询时(使用username@mydomain.com或username@mydomain.onmicrosoft.com)C# 使用MFA通过C访问Sharepoint online#,c#,sharepoint,C#,Sharepoint,我想使用非交互式C#程序从Sharepoint online下载一个文件。 最近启用了MFA,从那以后,我无法通过我的代码与任何用户一起获取文件,尽管我仍然可以通过门户网站访问它 首先,我尝试使用以下方法,获取的登录名或密码与Microsoft帐户系统中的登录名或密码不匹配。执行查询时(使用username@mydomain.com或username@mydomain.onmicrosoft.com) var ctx=newclientcontext(Properties.Settings.De
var ctx=newclientcontext(Properties.Settings.Default.SharepointBaseUrl)
{
凭证=凭证,
};
var-web=ctx.web;
ctx.Load(web);
尝试
{
ctx.ExecuteQuery();
}
捕获(例外情况除外)
{
返回字符串。空;
}
var fileUrl=$“{web.ServerRelativeUrl}/{file.Location}”;
var fi=Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx,fileUrl);
然后,我生成了一个AppId和AppSecret,并使用了以下代码:
var authenticationManager=new OfficeDevPnP.Core.authenticationManager();
var ctx=authenticationManager.GetAppOnlyAuthenticatedContext(
"https://mydomain.sharepoint.com/sites/defaultcollection/MyDir",
阿皮德,
appSecret);
但在尝试使用SharePoint.Client.file.OpenBinaryDirect(ctx,fileUrl)访问文件时,获得了一个401未经授权的
代码>使用File.OpenBinaryStream()代替如下:
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core;
using System.IO;
string siteUrl = "https://tenant.sharepoint.com/";
using (var ctx = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, "yourappid", "yourappsecret"))
{
ctx.Load(ctx.Web, p => p.Title);
ctx.ExecuteQuery();
Console.WriteLine(ctx.Web.Title);
Microsoft.SharePoint.Client.File file = ctx.Web.GetFileByUrl("https://tenant.sharepoint.com/Shared%20Documents/test.txt");
ctx.Load(file);
ctx.ExecuteQuery();
string filepath = @"C:\temp\" + file.Name;
Microsoft.SharePoint.Client.ClientResult<Stream> mstream = file.OpenBinaryStream();
ctx.ExecuteQuery();
using (var fileStream = new System.IO.FileStream(filepath, System.IO.FileMode.Create))
{
mstream.Value.CopyTo(fileStream);
}
};
使用Microsoft.SharePoint.Client;
使用OfficeDevPnP.Core;
使用System.IO;
字符串siteUrl=”https://tenant.sharepoint.com/";
使用(var ctx=new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl,“yourappid”,“yourappsecret”))
{
Load(ctx.Web,p=>p.Title);
ctx.ExecuteQuery();
Console.WriteLine(ctx.Web.Title);
Microsoft.SharePoint.Client.File文件=ctx.Web.GetFileByUrl(“https://tenant.sharepoint.com/Shared%20Documents/test.txt");
ctx.Load(文件);
ctx.ExecuteQuery();
字符串filepath=@“C:\temp\”+file.Name;
Microsoft.SharePoint.Client.ClientResult mstream=file.OpenBinaryStream();
ctx.ExecuteQuery();
使用(var fileStream=new System.IO.fileStream(filepath,System.IO.FileMode.Create))
{
mstream.Value.CopyTo(文件流);
}
};
使用File.OpenBinaryStream()代替如下方式:
using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core;
using System.IO;
string siteUrl = "https://tenant.sharepoint.com/";
using (var ctx = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, "yourappid", "yourappsecret"))
{
ctx.Load(ctx.Web, p => p.Title);
ctx.ExecuteQuery();
Console.WriteLine(ctx.Web.Title);
Microsoft.SharePoint.Client.File file = ctx.Web.GetFileByUrl("https://tenant.sharepoint.com/Shared%20Documents/test.txt");
ctx.Load(file);
ctx.ExecuteQuery();
string filepath = @"C:\temp\" + file.Name;
Microsoft.SharePoint.Client.ClientResult<Stream> mstream = file.OpenBinaryStream();
ctx.ExecuteQuery();
using (var fileStream = new System.IO.FileStream(filepath, System.IO.FileMode.Create))
{
mstream.Value.CopyTo(fileStream);
}
};
使用Microsoft.SharePoint.Client;
使用OfficeDevPnP.Core;
使用System.IO;
字符串siteUrl=”https://tenant.sharepoint.com/";
使用(var ctx=new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl,“yourappid”,“yourappsecret”))
{
Load(ctx.Web,p=>p.Title);
ctx.ExecuteQuery();
Console.WriteLine(ctx.Web.Title);
Microsoft.SharePoint.Client.File文件=ctx.Web.GetFileByUrl(“https://tenant.sharepoint.com/Shared%20Documents/test.txt");
ctx.Load(文件);
ctx.ExecuteQuery();
字符串filepath=@“C:\temp\”+file.Name;
Microsoft.SharePoint.Client.ClientResult mstream=file.OpenBinaryStream();
ctx.ExecuteQuery();
使用(var fileStream=new System.IO.fileStream(filepath,System.IO.FileMode.Create))
{
mstream.Value.CopyTo(文件流);
}
};
成功了!您想解释一下答案中的差异吗?这是因为在使用OAuth App Only凭据进行身份验证时无法使用File.OpenBinaryDirect,请检查此处的相同问题以了解详细信息:它有效!您想解释一下答案中的差异吗?这是因为在使用OAuth App Only凭据进行身份验证时无法使用File.OpenBinaryDirect,请检查此处的相同问题以获取详细信息: