C# Sharepoint REST api和MVC AAD connect

C# Sharepoint REST api和MVC AAD connect,c#,rest,sharepoint,azure-active-directory,microsoft-graph-api,C#,Rest,Sharepoint,Azure Active Directory,Microsoft Graph Api,我需要执行这个查询https://.sharepoint.com/_api/search/query?querytext=%27contenttype:articles%27 通过C语言中服务器端的Sharepoint REST api 我有来自MVC门户的Oauth2连接,所以我的目标是从连接中检索令牌,并将其作为承载令牌发送到sharepoint端点 我的意思是这样的 string userObjectID = ClaimsPrincipal.Current.FindFirst("htt

我需要执行这个查询https://.sharepoint.com/_api/search/query?querytext=%27contenttype:articles%27 通过C语言中服务器端的Sharepoint REST api

我有来自MVC门户的Oauth2连接,所以我的目标是从连接中检索令牌,并将其作为承载令牌发送到sharepoint端点

我的意思是这样的

  string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
  AuthenticationContext authContext = new AuthenticationContext(Startup.Authority, new NaiveSessionCache(userObjectID));
  ClientCredential credential = new ClientCredential(clientId, appKey);
  AuthenticationResult result = await authContext.AcquireTokenSilentAsync("https://<tenant>.sharepoint.com/", credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));

  HttpClient client = new HttpClient();
  HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "https://<tenant>.sharepoint.com/_api/search/query?querytext=%27contenttype:articles%27");
  request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
  HttpResponseMessage response = await client.SendAsync(request);
但是很明显,我找不到令牌

另一方面,我用ADv2和GraphServiceClient构建了一个运行良好的应用程序,但我不知道如何在图形模型中转换查询,而且我没有任何管理员同意

因此,我有两种方法来解决我的问题,我希望更好地使用microsoft graph api的第二个选项,但欢迎提供任何帮助

谢谢。

四处搜索 的功能有限,首先它将只在当前的网站集驱动器中搜索您的目标,其次我不确定目前它是否支持按内容类型搜索,可能带有$filter。。。 但如果这符合你的限制,这可能是一个更容易的选择

关于auth&auth 在graph或SharePoint search这两种情况下,当人们访问您的应用程序asp.net MVC时会发生的情况是,身份验证中间件负责将用户重定向到AAD,获取应用程序的访问令牌,将其重定向到应用程序,应用程序使用该访问令牌在应用程序上创建会话。 我的观点是:在这一点上,你所拥有的是:

应用程序的访问令牌不是图形,也不是SharePoint 针对您的应用程序的会话 要访问SharePoint/图表,您需要做几件事:

拦截并保留令牌服务器端是否将其添加到会话?如果您的中间件实现还没有做到这一点 使用该访问令牌+您的应用程序id/secret/certificate来获取针对AAD的SharePoint/graph的访问令牌 确保您的应用程序在AAD中具有与SharePoint/适当的图形API对话的权限 下面是一个示例,介绍如何使用从我拥有对我的应用程序/api的访问令牌到我拥有对图形/SharePoint的访问令牌。 注意:我在这里使用的是证书,但您可以使用机密


我没有提供关于如何截取令牌/在此处获取令牌的代码,因为您的问题不清楚您当前的身份验证和授权配置,以及您使用的asp.net core+中间件、classic+owin等MVC风格是什么?。我建议您从另一个问题开始,就这一具体问题提供更多细节。

感谢您的详细解释。首先,您能否确认MSAL Microsoft.Identity.Client是ADv2,ADAL Microsoft.IdentityModel.Clients.ActiveDirectory是旧协议?我已经设置了这两种方法,我的目标可能是使用MSAL ADv2构建所有方法,除非MSAL太年轻,无法使用。正如您在此处看到的链接,MSAL是Microsoft在使用v2端点时推荐的库,请不要忘了标记为答案-好的,这与我之前看到的相符,我怀疑的是你的答案上的url MSAL发送到ADAL Microsoft.IdentityModel.Clients.ActiveDirectory
var cac = new ClientAssertionCertificate(ApplicationId, CertificateProvider.AppCertificate);
            var ua = new UserAssertion(apiAccessToken);
            authenticationResult = await authContext.AcquireTokenAsync(resource, cac, ua);