Home > Software design >  How can I read user roles?
How can I read user roles?

Time:03-31

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

CodePudding user response:

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

CodePudding user response:

Are you missing a using directive for System.Linq?

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

  • Related