'How can I read user roles?

Currently I'm writing a TicketSystem in ASP.NET and have trouble with the Admin Panel.

I want read out the user roles and I'm getting the following error message:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: "'System.Threading.Tasks.Task<System.Collections.Generic.IList<string>>' does not contain a definition for 'ToList'"

Code to read the user role:

 public  List<String> getRols(dynamic user)
    {

        return _UserManager.GetRolesAsync(user).ToList();
       
    } 

Many Thanx for your Help



Solution 1:[1]

You're trying to access to add the ToList on a Task, which is something that doesn't exist. You either have to await the method, or take the result from the async call.

The best solution is the await solution, so it would be:

public async Task<List<string>> getRols(dynamic user)
{
    return await _UserManager.GetRolesAsync(user);
} 

Solution 2:[2]

Are you missing a using directive for System.Linq?

https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.tolist

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kevin
Solution 2 Andrew Morton