locked
What's the difference between using AuthenticationStateProvider and Task<AuthenticationState> to get the authenticated user? RRS feed

  • Question

  • User-1158769213 posted

    Not sure if it makes a difference, but I'm doing this in a Blazor component.

    I have seen two methods for getting the authenticated user in code...

    Method #1

        [Inject]
        private AuthenticationStateProvider _authenticationStateProvider { get; set; }
    
        protected override async Task OnInitializedAsync() {
          AuthenticationState authState = await _authenticationStateProvider.GetAuthenticationStateAsync();
          ClaimsPrinicpal jim = authState.User;
      }

    Method #2

        [CascadingParameter]
        private Task<AuthenticationState> _authState { get; set; }
    
        protected override async Task OnInitializedAsync() {
          ClaimsPrincipal jim = (await _authState).User;
        }

    As far as I can see, the two jims are the same. If so, is there any benefit to using one method over the other?

    Thanks

    Wednesday, December 16, 2020 8:02 PM

Answers

  • User1535942433 posted

    Hi Yossu,

    As far as I think,the drawback to using AuthenticationStateProvider directly is that the component isn't notified automatically if the underlying authentication state data changes.

    Task<AuthenticationState> Cascading Property - This property to get updated information about the user. So in any component to check user authenticated or to fetch claims of an authenticated user this cascading property need to be initialized in our components. We always prefer to create this cascading property instead of directly injecting AuthenticationStateProvider directly into our components.

    More details,you could refer to below article:

    https://docs.microsoft.com/en-us/aspnet/core/blazor/security/?view=aspnetcore-3.1#authenticationstateprovider-service

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 17, 2020 5:33 AM

All replies

  • User1535942433 posted

    Hi Yossu,

    As far as I think,the drawback to using AuthenticationStateProvider directly is that the component isn't notified automatically if the underlying authentication state data changes.

    Task<AuthenticationState> Cascading Property - This property to get updated information about the user. So in any component to check user authenticated or to fetch claims of an authenticated user this cascading property need to be initialized in our components. We always prefer to create this cascading property instead of directly injecting AuthenticationStateProvider directly into our components.

    More details,you could refer to below article:

    https://docs.microsoft.com/en-us/aspnet/core/blazor/security/?view=aspnetcore-3.1#authenticationstateprovider-service

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, December 17, 2020 5:33 AM
  • User-1158769213 posted

    That's great, thanks very much

    Thursday, December 17, 2020 2:15 PM