简体   繁体   中英

How can I get the current user name in the C# code behind of Blazor?

I have a Blazor server app with IIS Windows Authentication where I can get the current user in my razor page with following code, without problem:

<AuthorizeView>
        @context.User.Identity.Name
    </AuthorizeView>

I need this user info also in the code behind C# Class of the razor page (razor.cs) I tried this but I always get null as active user:

using Microsoft.AspNetCore.Components.Authorization;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
namespace WebApplication7.Pages
{  
public partial class Explorer
{
 public System.Security.Principal.IPrincipal User { get; set; }
 public static string CurrentUserName { get; set; }
 protected override async void OnInitialized() // = On Page Load
    {                   
        var authenticationState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
        CurrentUserName = authenticationState.User.Identity.Name;            
    }
 }
 }

I tried to use also 'context.User.Identity.Name' but I get always missing namespace error. What is wrong or missing in my code?

Here's a simple Razor file with two code behind files demonstrating two slightly different methods to get the user. The page displays the user name and the claims info for the user:

@page "/"
<h3>User</h3>
@if (this.user is not null && this.user.Identity is not null)
{
    <div class="p-2 m-2">
        <div class="p-2">
            Name: @user.Identity.Name
        </div>
        @foreach(var claim in this.user.Claims)
        {
        <div class="p-2">
            @claim.Type: @claim.Value
        </div>
        }
    </div>
}

The first code behind file shows how to use the cascading Task<AuthenticationState> (as the AuthorizeView component does).

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using System.Security.Claims;

namespace BlazorApp3.Pages
{
    public partial  class User: ComponentBase
    {
        [CascadingParameter] private Task<AuthenticationState> provider { get; set; } = default!;

        private ClaimsPrincipal? user;

        protected override async Task OnInitializedAsync()
        {
            var state = await provider;
            // This is the current logged in user
            user = state.User;
        }
    }
}

And a second version showing how to use the AuthenticationStateProvider DI service:

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;
using System.Security.Claims;

namespace BlazorApp3.Pages
{
    public partial  class User: ComponentBase
    {
        [Inject] public AuthenticationStateProvider AuthenticationStateProvider { get; set; } = default!;

        private ClaimsPrincipal? user;

        protected override async Task OnInitializedAsync()
        {
            var state = await AuthenticationStateProvider.GetAuthenticationStateAsync();
            // This is the current logged in user
            user = state.User;
        }
    }
}

You can also "inject" the service directly into the page like this:

@page "/"
@inject AuthenticationStateProvider AuthenticationStateProvider
<h3>User</h3>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM