【发布时间】:2018-12-14 10:49:26
【问题描述】:
我按照以下文档创建了带有 Azure AD 应用注册的 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 的字节数组来创建 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
执行查询中抛出错误
这里真的一无所知。
更新:
我在 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" />
【问题讨论】:
-
当最终用户同意多租户应用程序时,会在用户的租户中创建该应用程序的表示(服务主体)。如果应用程序是多层的(即它使用另一个应用程序,一个 API),您可以使用 knownClientApplications 告诉 AAD 也注册该服务主体。请在此处查看详细信息docs.microsoft.com/en-us/azure/active-directory/develop/…。那么,这些自动注册是否涵盖了您的用例(“我的最终目标是能够以编程方式创建 Azure AD 应用注册”)?
-
路易斯。 ADAL.NET 是多目标的。您能否检查所使用平台的 csproj - 查看 Microsoft.IdentityModel.Clients.ActiveDirector 的提示路径(它应该是 net45,而不是包下的另一个平台)。我提出这个建议的原因是,我们有时会看到 NuGet 包管理器在多目标 nuget 包的情况下选择了错误的平台。最后我可以看到有关 OfficeDevPnP.Core.AuthenticationManager 的信息吗?也许它使用 ADAL v2.0(与 ADAL v3.x 不兼容)
-
您好 Jean,感谢您的帮助,OfficeDevPnp 核心组件在这里:github.com/SharePoint/PnP-Sites-Core/tree/master/Core/…,在我的项目中我也有这个:
我的项目是 .net 4.6.1,关于平台它说 AnyCPU -
我想我知道问题出在哪里,但我不知道如何解决它,拳头基本上我的 asp.net webapi 受 Azure AD 保护,使用标准的 startup.auth.cs 机制:@ 987654324@,所以我的项目引用了该程序集,其次,我使用office PnP nuget 包进行Sharepoint 操作,如上面解释的第一个链接,使用AppOnly 策略我可以使用office pnp 执行任何sharepoint 操作。所以这里有一个 dll 冲突,我不知道如何解决。
-
@Jean-MarcPrieur 考虑到我最后的建议,解决此问题的最佳方法是什么?我在想也许不是下载nugetpackage,我可以手动下载office pnp核心包并更新引用的AD dll?
标签: c# .net azure azure-active-directory adal