Search code examples
asp.net-coreasp.net-identityrazor-pages

FindRoleById() of RoleManager return null when provided role id in a string variable (Identity 3.0)


I am facing a very peculiar problem while working with RoleManager of ASP.NET Core

I am getting a null value in the Role variable with the following code.

public IdentityRole Role { get; set; }

string id = "4de9af8e-c532-4d0a-8444-a63ffbd08ced";

Role = await _roleManager.FindByIdAsync(id);

However, it works fine when "role id" is provided directly to FindByIdAsync as shown below

Role = await _roleManager.FindByIdAsync("4de9af8e-c532-4d0a-8444-a63ffbd08ced");

Can anyone please help what can be the issue here? The same code has worked in different projects I worked on six months back.

I tried following

public IdentityRole Role { get; set; }

string id = "4de9af8e-c532-4d0a-8444-a63ffbd08ced";

Role = await _roleManager.FindByIdAsync(id);

Role is getting a null value whereas it should not have been null.


Solution

  • Try following

    Role = await _roleManager.FindByIdAsync(new Guid(id).ToString());