InvalidOperationException: No policy found: System.Object[] in .NET 6 project on Visual Studio 2022 Version 17.10.3

Madanraj A 40 Reputation points
2024-06-28T09:30:23.84+00:00

`I recently installed the latest version of Visual Studio 2022 Version 17.10.3 and started working on a .NET 6 project. When I run my project, I encounter the following exception:

InvalidOperationException: No policy found: System.Object[]. Microsoft.AspNetCore.Authorization.DefaultAuthorizationService.AuthorizeAsync(ClaimsPrincipal user, object resource, string policyName)

User's image

When I rollback to Visual Studio 2022 version 17.8.5, the project works without any issues. This leads me to believe that the problem might be related to the update to version 17.10.3

I have verified that the claim is present in the user's claims. However, I still get the InvalidOperationException.

What could be causing this issue in Visual Studio 2022 version version 17.10.3? How can I resolve this error so that my project runs correctly in the latest version of Visual Studio 2022 17.10.3?

Any help or insights would be greatly appreciated.

I noticed a potential difference in the AuthorizeAsync method implementation between the latest and older versions of the Microsoft.AspNetCore.Authorization namespace.

Latest Version:

public static Task<AuthorizationResult> AuthorizeAsync(this IAuthorizationService service, ClaimsPrincipal user, string policyName);


Older Version:


public static Task<AuthorizationResult> AuthorizeAsync(this IAuthorizationService service, ClaimsPrincipal user, string policyName)
{
    if (service == null)
    {
        throw new ArgumentNullException(nameof(service));
    }

    if (policyName == null)
    {
        throw new ArgumentNullException(nameof(policyName));
    }

    return service.AuthorizeAsync(user, resource: null, policyName: policyName);
}

It seems like the newer version might be missing some checks or default parameters that were present in the older version. Could this be related to the issue I'm experiencing?

What could be causing this issue in Visual Studio 2022 version 17.10.3? How can I resolve this error so that my project runs correctly in the latest version of Visual Studio 2022? Any help or insights would be greatly appreciated.`

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,338 questions
{count} votes

Accepted answer
  1. AgaveJoe 27,421 Reputation points
    2024-06-29T11:04:46.9266667+00:00

    I use AuthorizeAsync() is a .NET 6 project. My Visual Studio version is 17.10.3 and I do not have this problem.

    The error message indicate the string "System.Object[]" was passed to the policyName parameter.

    InvalidOperationException: No policy found: System.Object[].

    The error message also shows line106 in your source code where the variable passed to the string policyName parameter is policy.ToString(). I'm guessing that the "policy" variable is defined as a "var". The code populating the policy variable is probably populating policy with an object array because when an array of objects is converted to a string the result is "System.Object[]".

    var policy = new object[] { "one", "two" };
    Console.WriteLine(policy.ToString());
    
    

    Capture

    If "System.Object[]" is passed to policyName parameter then you get the the error message in your screenshot.

    Could you please advise on what might be causing this issue and how we can resolve it?

    I have to guess because the only source code provided is in the error message which is a screenshot. Find where "policy" is defined. Make sure policy is defined as a string not a var or something else. Once this change is made then you'll probably see a complier error in Visual Studio because whatever is populating policy needs to be updated as well.

    Also, remove the .ToString() from policy.ToString() in line 106.

    Lastly, Visual Studio has a wonderful debugger. Set a breakpoint on line 106 and use the debugger's locals or watch window to view the actual value of "policy"

    https://video2.skills-academy.com/en-us/visualstudio/debugger/debugger-feature-tour?view=vs-2022

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful