Ready to post? First, try searching for your answer.
Hi there … I’m not sure what I’m doing wrong but here it goes …
I created my roles and granted my Users access for my Blazor Server app:
HOWEVER when I check the jwt, there are no roles:
“http://schemas.microsoft.com/ws/2008/06/identity/claims/role”: ,
“nickname”:“username” etc.
However, when I run the following code after I’ve already logged in, I get the roles:
UserId = state.User.Claims
.Where(c => c.Type.Contains(“nameidentifier”))
.Select(c => c.Value)
.FirstOrDefault() ?? string.Empty;
GetRoles(UserId,GetBearerToken());
private string GetBearerToken()
{
var client = new RestClient(“https://[Domain]/oauth/token”);
var request = new RestRequest();
request.Method = Method.Post;
request.AddHeader("content-type", "application/json");
request.AddParameter("application/json", "{\"client_id\":\"[client_id]\",\"client_secret\":\"[client_secret]\",\"audience\":\"https://[domain]/api/v2/\",\"grant_type\":\"client_credentials\"}", ParameterType.RequestBody);
var response = client.Execute(request);
var content = response.Content ?? string.Empty;
var bearerToken = JsonNode.Parse(content)?["access_token"];
return bearerToken?.ToString() ?? string.Empty;
}
private async void GetRoles(string userId, string token)
{
if (userId.Trim().Length > 0 && token.Trim().Length > 0)
{
var client = new RestClient(“https://[domain]/api/v2/users/” + userId + “/roles”);
var request = new RestRequest();
request.Method = Method.Get;
request.AddHeader("authorization", "Bearer " + token);
var response = client.Execute(request);
List<ApplicationRole> roles = JsonSerializer.Deserialize<List<ApplicationRole>>(response.Content);
if (roles != null)
{
foreach (ApplicationRole role in roles)
Console.WriteLine(role.name);
}
//SetRoles();
}
}
class ApplicationRole
{
public string id { get; set; }
public string name { get; set; }
public string description { get; set; }
}