Save $100 with our Black Friday deals!

Get $100 OFF with promo code BLACKFRIDAY2024 at checkout until November 30.

See Pricing

Still using Radzen Studio?
Achieve more with Radzen Blazor Studio

Radzen Blazor Studio is our new flagship product and vision of how rapid Blazor application development should be done.

Go to Radzen Blazor Studio

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