Get current user | Blazor (Blazor)

The current application user is available as the User property of the SecurityService class. An instance of this class is available in Blazor components which Radzen generates from the application pages.

Here is how to inject the SecurityService in custom classes.

  1. Add a field of type SecurityService
    private readonly SecurityService security;
    
  2. Add a constructor parameter of type SecurityService.
    public MyService(SecurityService security)
    {
    }
    
  3. Assign the field to the parameter.
    public MyService(SecurityService security)
    {
       this.security = security;
    }
    

If you are extending one of the services generated by Radzen make sure to invoke the generated constructor and add all existing parameters e.g.

public partial class CrmService
{
    private readonly SecurityService security;

    public CrmService(/* add the context parameter */CrmContext context, SecurityService security)
      : /*invoke the generated constructor */this(context)
    {
        this.security = security;
    }
}

After you have injected the SecurityService you can use its properties and methods:

var user = security.User;
var isAdmin = security.IsInRole("Administrator");