Lui*_*cia 15 .net c# azure azure-active-directory adal
我按照以下文档使用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");
}
Run Code Online (Sandbox Code Playgroud)
我正在使用来自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
Run Code Online (Sandbox Code Playgroud)
堆栈跟踪:
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
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
这在我的packages.config上
<package id="Microsoft.IdentityModel.Clients.ActiveDirectory" version="3.19.5" targetFramework="net461" />
Run Code Online (Sandbox Code Playgroud)
PnP Core 当前使用旧版本2.29.0
的Microsoft.IdentityModel.Clients.ActiveDirectory
包。
最好是将 API 项目中使用的版本降级为 PnP 框架使用的版本,这肯定会起作用。
与错误相关的是,框架正在AuthenticationContext.AcquireToken
内部调用该方法,该方法已在较新的 Nuget v3 包中弃用。
因此,看起来 PnP 代码正在调用此方法属于 v2 版本,而使用 v3 包的正确方法是AcquireTokenAsync
v3 版本,从而导致冲突。
参考 - OfficeDevPnP Core package.config文件
现在,我们可以看到已经是一个PR来更新 PnP 框架本身中的 nuget 包,当它被接受时将立即解决您的问题。但可能需要一些时间才能被接受,所以不要屏住呼吸:)
ADAL 3.x 中已弃用 AuthenticationResult.AcquireToken 以及如何修复。
Microsoft Docs - AuthenticationContext.AcquireTokenAsync 方法
所以最好是将 API 项目降级到 v2,或者等待 PnP 框架升级包及其必要的依赖项。
另一种选择,如果您只使用 PnP 进行身份验证,那么您可以使用以下不需要您更改包的辅助方法。但是,如果您使用其他功能,如配置或其他扩展,那么不幸的是您需要将其降级。这是从 PnP 本身内部使用的内容修改而来的,以利用 v3 包更改:
public ClientContext GetAzureADAppOnlyAuthenticatedContext(string siteUrl, string clientId, string tenant, X509Certificate2 certificate)
{
var clientContext = new ClientContext(siteUrl);
string authority = string.Format(CultureInfo.InvariantCulture, "https://login.windows.net/{0}/", tenant);
var authContext = new AuthenticationContext(authority);
var clientAssertionCertificate = new ClientAssertionCertificate(clientId, certificate);
var host = new Uri(siteUrl);
clientContext.ExecutingWebRequest += (sender, args) =>
{
var ar = authContext.AcquireTokenAsync(host.Scheme + "://" + host.Host + "/", clientAssertionCertificate).GetAwaiter().GetResult();
args.WebRequestExecutor.RequestHeaders["Authorization"] = "Bearer " + ar.AccessToken;
};
return clientContext;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1807 次 |
最近记录: |