0

I am trying to grab the user that is logged in the Blazor app on .NET Core 5.0 by doing just the cmd @context.User.Identity.Name, but when I run the program it just shows "Welcome, "

enter image description here

So I then saw a potential work around for this which is to Inject a AuthenticationStateProvider property, call GetAuthenticationStateAsync and get User from it.

Attempting to do so:

Index.razor:

@page "/"
@inject AuthenticationStateProvider GetAuthenticationStateAsync

<AuthorizeView>
    <Authorized>
        <h3>Welcome, <b>@name</b></h3>
    </Authorized>
    <NotAuthorized>
        <h3>You are signed out!!</h3>
    </NotAuthorized>
</AuthorizeView>

@code{

    protected override async Task OnInitializedAsync()
    {
        var authstate = await GetAuthenticationStateAsync.GetAuthenticationStateAsync();
        var user = authstate.User;
        var name = user.Identity.Name;
    }
}

The issue is, whenever I do @name in the <h3> it says the name 'name' does not exist in the current context. I even tried moving the @code {} before the <AuthorizeView> but it still says the same thing. Is there a reason why I cannot call the @name?

FOLLOW UP

I attempted to do the following inside of my Index.razor and doing it this way did not work, still displayed as in the screenshot. Any help would be greatly appreciated!

@page "/"
@inject AuthenticationStateProvider GetAuthenticationStateAsync

<AuthorizeView>
    <Authorized>
        <h3>Welcome, <b>@GetAuthenticationStateAsync.GetAuthenticationStateAsync().Result.User.Identity.Name</b></h3>
    </Authorized>
    <NotAuthorized>
        <h3>You are signed out!!</h3>
    </NotAuthorized>
</AuthorizeView>

DEBUG ATTEMPT

enter image description here

SECOND ATTEMPT, I still get the same result as in the screenshot when doing this when I attempt to use Rene's suggestion

@page "/"
@inject AuthenticationStateProvider GetAuthenticationStateAsync

<AuthorizeView>
    <Authorized>
        <h3>Welcome, <b>@Name</b></h3>
    </Authorized>
    <NotAuthorized>
        <h3>You are signed out!!</h3>
    </NotAuthorized>
</AuthorizeView>

@code{

    private string Name;

    protected override async Task OnInitializedAsync()
    {
        var authstate = await GetAuthenticationStateAsync.GetAuthenticationStateAsync();
        var user = authstate.User;
        var name = user.Identity.Name;
        Name = name;
    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
NoviceCoder
  • 449
  • 1
  • 9
  • 26
  • 1
    The issue is not with the current code you use. The issue is that no authentication state is created for your app. I've been following your series of questions...let me suggest that you start here: Search Google for the string "stackoverflow enet blazor openid connect", inspect my answers and related answers, and try to improve on your app – enet Jun 30 '21 at 18:49
  • 1
    Thank you for your suggestion! I have authorization implemented in my blazor project. When I run the blazor program, I click on login in the nav menu which will redirect me to my identityserver project which then I login after I login it redirects me back into the blazor project home page. @enet – NoviceCoder Jun 30 '21 at 19:31

2 Answers2

3

Your name variable is declared within the scope of the method. Just declare a Name property above the method override just as you would in a class. Then you can Set its value in the method and read it in razor.

René
  • 3,413
  • 2
  • 20
  • 34
  • I attempted this and I still am not getting anything returned. Please see the updated post where I attempt to do this. – NoviceCoder Jun 30 '21 at 18:05
1

It looks like your issue might be that there is no data stored in "@GetAuthenticationStateAsync.GetAuthenticationStateAsync().Result.User.Identity.Name". Try running in debug with a breakpoint at that location and see if there's any value stored in Name.

M. Dave
  • 36
  • 3
  • Hmm, I am thinking that could possibly be the case now that you mention it. I attempted to debug it, however when I set a breakpoint on that line it says The breakpoint will not currently be hit. No executable code on the debugger's target code type is associated with this line. Is there a way to debug a razor component? I will upload the full message on the original post. Please see it. – NoviceCoder Jun 30 '21 at 18:02
  • @M. Dave, the link you provided should be removed from your answer. The guys in Syncfusion are so confused that they have never heard that HttpContext is not available in Blazor. There are good answers in Stackoverflow you can link to. – enet Jun 30 '21 at 18:34
  • @enet Edited out. – M. Dave Jun 30 '21 at 19:41
  • @M.Dave After getting Debug to work, name is null. Not sure how that is if I logged in through the identityserver and became authorized. – NoviceCoder Jun 30 '21 at 20:15