Access to XMLHttpRequest at...blocked by CORS policy

Greetings...
Working through how to Authenticate w/ Radzen using Zenfolio API (documentation here:http://api.zenfolio.com/api/1.8/zfapi.asmx?op=AuthenticatePlain) and I've hit a wall.

I've called the 'AuthenticatePlain' resource on the ZF Api, but I am getting this error when I execute w/ Radzen.
"Access to XMLHttpRequest at...has been blocked by CORS policy: Request header field pragma is not allowed by Access-Control-Allow-Headers in preflight response."

...but the same request works in Postman and returns the AuthToken I'm trying to retrieve.

I've seen similar post/questions on this...but as I am completely new to Radzen, I wanted to make sure that I was not missing something.

Is there anything I can do on my end to resolve this or is this 100% on the API side?

Thank you!

The CORS policy isn't something caused by or related to Radzen. All browsers obey the same origin policy (Postman is not a browser and doesn't play by those rules).

The CORS is a way for 3rd party servers to allow access to browsers. It seems that the API you are using doesn't support CORS though. A possible solution is to make the API calls server-side by using custom methods.

Thank you! That's what I figured. :frowning:
I've contacted support on the ZF API side...I don't know if that a "simple" configuration change they can make, but it can't hurt to ask.

Thanks for this suggestion.

I've added a project w/ a web-reference to the API that I am trying to use. I then followed the example and invoked a simple "GetAuthToken" method but it appears that does not work either...

...if I've gotten this far, I'm guessing (hoping) I did it correctly. Is there anything else that I might be able to try?

There is probably a server-side error - check Radzen's output pane for any exception stacktraces (you can also attach the Visual Studio debugger).

Here is the output from Radzen. I was not able to debug on the VS2017 side...

radzen: Generating code ...
radzen: Code generation done in 335ms.
dotnet: C:\Program Files\dotnet\sdk\2.2.104\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.ObsoleteReferences.targets(33,5): warning NETSDK1059: The tool 'Microsoft.EntityFrameworkCore.Tools.DotNet' is now included in the .NET Core SDK. Information on resolving this warning is available at (Breaking changes in .NET Core 2.1 - .NET | Microsoft Learn). [D:\UserData\Documents\Radzen Projects\zfDemo_dotNet2\server\project.csproj]

dotnet: C:\Program Files\dotnet\sdk\2.2.104\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.ObsoleteReferences.targets(33,5): warning NETSDK1059: The tool 'Microsoft.EntityFrameworkCore.Tools.DotNet' is now included in the .NET Core SDK. Information on resolving this
dotnet: warning is available at (Breaking changes in .NET Core 2.1 - .NET | Microsoft Learn). [D:\UserData\Documents\Radzen Projects\zfDemo_dotNet2\server\project.csproj]

dotnet: C:\Program Files\dotnet\sdk\2.2.104\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Sdk.DefaultItems.targets(153,5): warning NETSDK1071: A PackageReference to 'Microsoft.AspNetCore.All' specified a Version of 2.0.0. Specifying the version of this package is
dotnet: not recommended. For more information, see .NET project SDK overview | Microsoft Learn [D:\UserData\Documents\Radzen Projects\zfDemo_dotNet2\server\project.csproj]

dotnet: info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
User profile is available. Using 'C:\Users\Isaac2\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.

dotnet: Hosting environment: Development

dotnet: Content root path: D:\UserData\Documents\Radzen Projects\zfDemo_dotNet2\server

dotnet: Now listening on: http://localhost:5000

dotnet: Application started. Press Ctrl+C to shut down.

ng-cli: ** NG Live Development Server is listening on localhost:8000, open your browser on http://localhost:8000/ **

ng-cli: webpack: wait until bundle finished: /

ng-cli: Date: 2019-02-27T17:54:23.616Z
Hash: d1ff49e6e9b5380a38ec
Time: 21953ms
chunk {inline} inline.bundle.js (inline) 5.79 kB [entry] [rendered]
chunk {main} main.bundle.js (main) 93.7 kB [initial] [rendered]
chunk {polyfills} polyfills.bundle.js (polyfills) 1.25 MB [initial] [rendered]
chunk {styles} styles.bundle.js (styles) 693 kB [initial] [rendered]
chunk {vendor} vendor.bundle.js (vendor) 11.1 MB [initial] [rendered]

ng-cli:

ng-cli: webpack: Compiled successfully.

dotnet: info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 OPTIONS http://localhost:5000/api/ServerMethods/GetZfAuthToken?userName=[**MASKED**]&password=[**MASKED**]

dotnet: info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 OPTIONS http://localhost:5000/api/ServerMethods/GetZfAuthToken?userName=[**MASKED**]&password=[**MASKED**]

dotnet: infoinfo: Microsoft.AspNetCore.Cors.Infrastructure.CorsService[4]
Policy execution successful.
: Microsoft.AspNetCore.Cors.Infrastructure.CorsService[4]
Policy execution successful.

dotnet: info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 105.3831ms 204

dotnet: info
dotnet: : Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 105.3831ms 204

dotnet: info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 GET http://localhost:5000/api/ServerMethods/GetZfAuthToken?userName=[**MASKED**]&password=[**MASKED**]

dotnet: info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 GET http://localhost:5000/api/ServerMethods/GetZfAuthToken?userName=[**MASKED**]&password=[**MASKED**]

dotnet: info: Microsoft.AspNetCore.Cors.Infrastructure.CorsService[4]
Policy execution successful.

dotnet: info: Microsoft.AspNetCore.Cors.Infrastructure.CorsService[4]
Policy execution successful.

dotnet: infoinfo: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
Executing action method ZdDemoDotNet2.Controllers.ServerMethodsController.GetZfAuthToken (server) with arguments ([MASKED], [MASKED]) - ModelState is Valid
: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
Executing action method ZdDemoDotNet2.Controllers.ServerMethodsController.GetZfAuthToken (server) with arguments ([MASKED], [MASKED]) - ModelState is Valid

dotnet: info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
Executed action ZdDemoDotNet2.Controllers.ServerMethodsController.GetZfAuthToken (server) in 132.9677ms
info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
Executed action ZdDemoDotNet2.Controllers.ServerMethodsController.GetZfAuthToken (server) in 132.9677ms

dotnet: fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[0]
An unhandled exception has occurred while executing the request
System.BadImageFormatException: Could not load file or assembly 'System.Web.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)
File name: 'System.Web.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' ---> System.BadImageFormatException: Cannot load a reference assembly for execution.
at ZdDemoDotNet2.Controllers.ServerMethodsController.GetZfAuthToken(String userName, String password)
at lambda_method(Closure , Object , Object )
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__10.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Builder.RouterMiddleware.d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.d__7.MoveNext()

dotnet: fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[0]
An unhandled exception has occurred while executing the request
System.BadImageFormatException: Could not load file or assembly 'System.Web.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)
File name: 'System.Web.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' ---> System.BadImageFormatException: Cannot load a reference assembly for execution.
at ZdDemoDotNet2.Controllers.ServerMethodsController.GetZfAuthToken(String userName, String password)
at lambda_method(Closure , Object , Object )
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__12.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__10.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Rethrow(ActionExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.d__14.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__22.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__17.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.d__15.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Builder.RouterMiddleware.d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.d__7.MoveNext()

dotnet: info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 677.899ms 500 text/html; charset=utf-8

dotnet: info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 677.899ms 500 text/html; charset=utf-8

I think the API doesn't support .NET Core hence the BadImageFormatException.

Maybe I should backtrack a little...here's my setup thus far.

I added a project "zfApi" to "application.sln".
I added zfApi as a project dependency.
zfApi contains the web-reference to the actually Zenfolio API.
This results in the error noted in the screen shot.
When I add an assembly reference to "project" for 'System.Web.Services" it will compile w/ no errors.

Execution, at this point, results in the error you mentioned.

  An unhandled exception has occurred while executing the request

System.BadImageFormatException: Could not load file or assembly 'System.Web.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. Reference assemblies should not be loaded for execution. They can only be loaded in the Reflection-only loader context. (Exception from HRESULT: 0x80131058)

All this to say...based on this, did I set this up correctly? It's hard for me to tell where the error is really coming from and I have a suspicion that I'm doing something wrong...that it may not be the API causing the error.

I don't think you are doing something wrong. I think the sample and the API you are trying to use are not meant for .NET Core but for regular .NET. I suggest you ask Zenfolio for a .NET Core 2.0 demo - this would be usable in Radzen applications.