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();
}
}
}