PlatformNotSupportedException: Secure binary serialization is not supported on this platform

Viewed 1645

Trying to implement custom authorization attribute converting from WebApi 2.0 to .Net Core 3.1 and Microsoft.AspnetCore.Odata 7.4.1 on WebApi and Microsoft.Odata.Client 7.7.0.

I revamped this code from a working OData – API in .NET 4.8, and everything works until we apply the custom authorization attribute to the endpoint.

Getting the following error on the client when returning UnauthorizedResult from custom authorization attribute which is used on Odata controller. Also tried to inherit from AuthorizeAttribute instead of Attribute, IAuthorizationFilter and getting the same error.

Error: An unhandled exception occurred while processing the request. PlatformNotSupportedException: Secure binary serialization is not supported on this platform. System.Exception.add_SerializeObjectState(EventHandler value)

[CustomAuthorization]
[ODataRoutePrefix("Data")]
public class DataController: ODataController
{
}



[AttributeUsage(AttributeTargets.Class, Inherited = true, AllowMultiple = false)]
public class CustomAuthorizationAttribute : Attribute, IAuthorizationFilter
{
   public void OnAuthorization(AuthorizationFilterContext filterContext)
          {
             if (!ValidToken(filterContext, sUserToken))
               {         
                  filterContext.HttpContext.Response.Headers.Add("AuthorizationStatus","NotAuthorized");
                  filterContext.Result = new UnauthorizedResult();
                }
           }
 }
1 Answers

It looks like recent work in OData Client for v7.7.0 has altered how error response are handled and how exceptions are deserialized, I would go so far as to suggest this is a bug. I can't find any direct evidence of this change in the release notes other than this issue with deserializing exceptions

Downgrading OData Client to v7.6.4 will generally resolve this.

Please contribute to this Git Hub issue #1833 that is the same underlying issue.

Related