Get current user (Angular)

Radzen provides security support out of the box, more info can be found in our Security article. This guide demonstrates how to get current user.

Source Code

Step by step

1. Create new application with .NET server-side project, add new MSSQL data-source connected to Northwind database, auto-generate pages and enable security.

2. To get the name of the authenticated user you can do the following:

var userName = this.HttpContext.User.FindFirst(ClaimTypes.Name).Value;
Dim userName = Me.HttpContext.User.FindFirst(ClaimTypes.Name).Value

For .NET Core 2.x you need to decorate your class with [Authorize] attribute in order to access current authenticated user:

using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;

namespace [ApplicationName].Controllers.[DataSourceName]
{
    [Authorize(AuthenticationSchemes="Bearer")]
    public partial class OrdersController
    {
    }
}
Imports System.Security.Claims
Imports System.Threading.Tasks
Imports Microsoft.AspNetCore.Authorization
Imports Microsoft.AspNetCore.Identity

Namespace [ApplicationName].Controllers.[DataSourceName]
    <Authorize(AuthenticationSchemes:="Bearer")>
    Public Partial Class OrdersController
    End Class
End Namespace

3. To authorize only certain roles to have access to controllers you can use again the Authorize attribute:

using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;

namespace [ApplicationName].Controllers.[DataSourceName]
{
    [Authorize(Roles="Administrator", AuthenticationSchemes="Bearer")]
    public partial class OrdersController
    {
    }
}
Imports System.Security.Claims
Imports System.Threading.Tasks
Imports Microsoft.AspNetCore.Authorization
Imports Microsoft.AspNetCore.Identity

Namespace [ApplicationName].Controllers.[DataSourceName]
    <Authorize(Roles:="Administrator", AuthenticationSchemes:="Bearer")>
    Public Partial Class OrdersController
    End Class
End Namespace

4. Run the application, login and check current user.