无法从程序集'System.Core,Version = 4.0.0.0,Culture = neutral,PublicKeyToken ='加载类型'System.Security.Cryptography.SHA256Cng'

时间:2019-04-23 08:39:26

标签: c# asp.net-core-2.1 dynamics-365 dynamics-crm-webapi

我试图将.NET Core Web应用程序与Dynamics 365 API连接起来。我使用了一种称为Auth的方法。该方法基于作为参数给出的查询从Dynamics 365读取。

 public static async Task<JObject> Auth(string query)
        {

            string api = "https://mydomain.api.crm4.dynamics.com/api/data/v9.1/";

            AuthenticationParameters ap = AuthenticationParameters.CreateFromResourceUrlAsync(new Uri(api)).Result;

            var creds = new ClientCredential("application Id", "client key");

            AuthenticationContext authContext = new AuthenticationContext(ap.Authority);
            var token = authContext.AcquireTokenAsync(ap.Resource, creds).Result.AccessToken;

            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.Timeout = new TimeSpan(0, 2, 0);
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);

                HttpResponseMessage response = await httpClient.GetAsync(api + query);
                if (response.StatusCode == HttpStatusCode.OK)  //200
                {
                    JObject RetrievedVersion = JsonConvert.DeserializeObject<JObject>(
                        await response.Content.ReadAsStringAsync());
                    return RetrievedVersion;                  

                }
                else
                {
                   return (JObject)"Failed to retrieve the version for some reason";
                }               
            }
}
在以下代码行中,将

Auth(query)调用到控制器中的一个动作中:

 ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
 var execute = Task.Run(async () => await Auth("/contacts?$select=fullname")).Result;

但是,我遇到以下错误:

An unhandled exception occurred while processing the request.
AggregateException: One or more errors occurred. (One or more errors occurred. (Could not load type 'System.Security.Cryptography.SHA256Cng' from assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.))
System.Threading.Tasks.Task<TResult>.GetResultCore(bool waitCompletionNotification)

TypeLoadException: Could not load type 'System.Security.Cryptography.SHA256Cng' from assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.
Microsoft.IdentityModel.Clients.ActiveDirectory.PlatformSpecificHelper.CreateSha256Hash(string input)

Stack Query Cookies Headers
AggregateException: One or more errors occurred. (One or more errors occurred. (Could not load type 'System.Security.Cryptography.SHA256Cng' from assembly 'System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.))
System.Threading.Tasks.Task<TResult>.GetResultCore(bool waitCompletionNotification)
Projectname.Controllers.HomeController.Contacts() in HomeController.cs
+
            var execute = Task.Run(async () => await Auth("/contacts?$select=fullname")).Result;
lambda_method(Closure , object , object[] )
Microsoft.Extensions.Internal.ObjectMethodExecutor.Execute(object target, object[] parameters)
Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor+SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeActionMethodAsync()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeNextActionFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.InvokeInnerFilterAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.MigrationsEndPointMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore.DatabaseErrorPageMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

代码运行良好,并且可以在控制台应用程序上完美地从Dynamics 365提取数据。

我尝试添加软件包System.Security.Cryptography.Cng,但是尝试解决该问题未成功。

这是在.NET Core 2.1 MVC Web应用程序中实现Dynamics 365 API的方法吗?如果是这样,如果您中有人可以向我解释如何解决此错误,我将不胜感激。否则,您的建议确实可以帮助我解决此问题。

谢谢。

0 个答案:

没有答案