我按照以下文档使用Azure AD App Registration创建了x509证书 .
https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azuread
我生成了.pfx文件,设置了密码,我还在我的租户Azure AD中注册了该应用程序,然后使用keycredentials部分更新了清单 .
然后,我正在创建一个接收一些参数的WEB API,包括.pfx文件 .
[HttpPut]
public async Task<IHttpActionResult> PutTenant([ModelBinder(typeof(TenantModelBinder))] Tenant tenant)
{
try
{
var cert = new X509Certificate2(tenant.CertificateFile, tenant.CertificatePassword, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
using (var cc = new OfficeDevPnP.Core.AuthenticationManager().GetAzureADAppOnlyAuthenticatedContext(tenant.SiteCollectionTestUrl, tenant.ApplicationId, tenant.TenantDomainUrl, cert))
{
cc.Load(cc.Web, p => p.Title);
cc.ExecuteQuery();
};
}
catch (System.Exception)
{
return BadRequest("Configuration Invalid");
}
我正在使用来自HttpRequest的bytearray来创建x509对象 .
但是我收到此错误:
Message "Method not found: 'Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationResult Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext.AcquireToken(System.String, Microsoft.IdentityModel.Clients.ActiveDirectory.ClientAssertionCertificate)'." string
堆栈跟踪:
at OfficeDevPnP.Core.AuthenticationManager.<>c__DisplayClass36_0.<GetAzureADAppOnlyAuthenticatedContext>b__0(Object sender, WebRequestEventArgs args)\r\n at Microsoft.SharePoint.Client.ClientRuntimeContext.OnExecutingWebRequest(WebRequestEventArgs args)\r\n at Microsoft.SharePoint.Client.ClientContext.GetWebRequestExecutor()\r\n at Microsoft.SharePoint.Client.ClientContext.GetFormDigestInfoPrivate()\r\n at Microsoft.SharePoint.Client.ClientContext.EnsureFormDigest()\r\n at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()\r\n at TenantManagementWebApi.Controllers.TenantController.<PutTenant>d__2.MoveNext() in C:\\Users\\levm3\\source\\repos\\TenantManagementWebApi\\Controllers\\TenantController.cs
executequery中抛出错误
这里真的很无能为力 .
更新:
我在web.config中注意到了这一点
<dependentAssembly>
<assemblyIdentity name="Microsoft.IdentityModel.Clients.ActiveDirectory" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-3.19.5.13701" newVersion="3.19.5.13701"/>
</dependentAssembly>
这在我的packages.config上
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.19.5" targetFramework="net461" />
1 回答
PnP Core目前使用
Microsoft.IdentityModel.Clients.ActiveDirectory
包的旧版本2.29.0
.最好的办法是将API项目中使用的版本降级为PnP Framework使用的版本 .
与错误相关,框架在内部调用
AuthenticationContext.AcquireToken
方法,该方法已在较新的Nuget v3包中弃用 .因此,看起来PnP代码调用此方法属于v2版本,而使用的v3包的正确方法是v3版本的
AcquireTokenAsync
导致冲突 .参考 - OfficeDevPnP Core package.config文件
现在,我们可以看到已经有了PR来更新PnP框架本身的nuget软件包,当被接受时将立即解决您的问题 . 但可能需要一些才能被接受,所以不要屏住呼吸:)
AuthenticationResult.AcquireToken deprecated in ADAL 3.x and how to fix.
Microsoft Docs - AuthenticationContext.AcquireTokenAsync Method
因此,最好将API项目降级为v2或等待PnP Framework升级软件包及其必要的依赖项 .
另一个选择,如果您只使用PnP进行身份验证,那么您可以使用下面的帮助程序方法,它不需要您更改程序包 . 但是,如果您使用其他功能(如配置或其他扩展),那么您需要降级它 . 这是根据PnP内部使用的内容修改的,以便使用v3包更改: