title: Custom AuthenticationStateProvider returns "empty user"
body: <p>in our application I'd like to use the user management of our fat client. For this I have written a custom AuthenticationStateProvider:</p>
  
  <pre><code>public class MyAuthenticationStateProvider : ServerAuthenticationStateProvider, IAuthentorizationService, IDisposable
  {
      public MyAuthenticationStateProvider (IPermissionManager permissionManager)
      {
           //User management service of the fat client
          _permissionManager = permissionManager;
      }
  
      public override Task&lt;AuthenticationState&gt; GetAuthenticationStateAsync()
      {
          if (_permissionManager.PermissionUser == null)
          {
              var emptyUser = new ClaimsPrincipal(new ClaimsIdentity(new Claim[0]));
              return Task.FromResult(new AuthenticationState(emptyUser));
          }
          var identity = new ClaimsIdentity(new[]
          {
              new Claim(ClaimTypes.Name, _permissionManager.PermissionUser.User.GetName())
          }, "FatClientAuthentication");
          var user = new ClaimsPrincipal(identity);
          return Task.FromResult(new AuthenticationState(user));
      }
  
      public async Task&lt;bool&gt; LoginUser(string userName, string password)
      {
          //Login via WCF connection
          var response = await _clientProxy.Login(new LoginRequest
          {
              LoginUserName = userName,
              Password = password
  
          });
          response.LogExceptionIfFaulted(_logger);
          if (response.Ok)
          {
              _permissionManager.Initialize(response.LoggedInUser);
              NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
          }
          return response.Ok;
      }
  </code></pre>
  
  <p>The login works fine. For testing purposes I always log in with fixed user credentials. After the successful login I fire the NotifyAuthenticationStateChanged event, which results in a correct call of the GetAuthenticationStateAsync method. The now logged in user is correctly wrapped inside the AuthenticationState. When debugging the code I can see that the Identity with the name claim is the correct user and the IsAuthenticated property is true.</p>
  
  <p>However, when using the "AuthorizeView" component, I always get an "empty user" (no name claim, no user name, IsAuthenticated is false)</p>
  
  <p>I now have a small component just for testing: </p>
  
  <pre><code>&lt;AuthorizeView&gt;
  &lt;Authorized&gt;
      &lt;h2&gt;User @context.User.Identity.Name&lt;/h2&gt; is logged in!
      Claims:
      &lt;ul&gt;
          @foreach (var claim in context.User.Claims)
          {
              &lt;li&gt;Type=@claim.Type; Value=@claim.Value&lt;/li&gt;
          }
      &lt;/ul&gt;
      @context.User.Claims
      &lt;p&gt;Current count: @currentCount&lt;/p&gt;
  
      &lt;button class="btn btn-primary" @onclick="IncrementCount"&gt;Click me&lt;/button&gt;
  &lt;/Authorized&gt;
  &lt;NotAuthorized&gt;
      &lt;h2&gt;User @context.User.Identity.Name&lt;/h2&gt; @*this is an empty string*@
      &lt;h2&gt;Authentication Type: @context.User.Identity.AuthenticationType&lt;/h2&gt; @*empty*@
      &lt;h2&gt;Authenticated: @context.User.Identity.IsAuthenticated&lt;/h2&gt;@*false*@
      No user is logged in!
  &lt;/NotAuthorized&gt;
  </code></pre>
  
  <p></p>
  
  <p>Im using the AuthorizeRouteView and the CascadingAuthenticationState in the App.razor like in the official sample displayed in <a href="https://docs.microsoft.com/en-us/aspnet/core/security/blazor/?view=aspnetcore-3.1" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/aspnet/core/security/blazor/?view=aspnetcore-3.1</a></p>
  
  <p>Accessing the AuthenticationState via an CascadingParameter also results in the same "empty user".</p>
  
  <p>Appreciate any help,</p>
  
  <p>tilt32</p>
  
  <p><strong><em>EDIT 1</em></strong></p>
  
  <p>So I looked into the login behaviour again, making sure that the event is called.
  I then figured out, that my AuthenticationStateChanged event has no subscribers (is null). My impression was, that something in the framework attaches to this event at startup. Maybe I did forget some configuration method call in the startup ? This is what I do in the configure services:</p>
  
  <pre><code>    services.AddScoped&lt;AuthenticationStateProvider, MyAuthenticationStateProvider&gt;();
              services.AddScoped&lt;ServerAuthenticationStateProvider, MyAuthenticationStateProvider&gt;();
  //Interface which I use in my LoginCompontent and at Startup to log in with the default user or some real user credentials
              services.AddScoped&lt;IAuthenticationService, MyAuthenticationStateProvider&gt;();
  </code></pre>
  
  <p>I also tried the approach suggested by user enet. Sadly with no success, the result was the same. During the login a call to NotifyAuthenticationStateChanged and hence to the event with no subscribers is done.</p>
  
  <p>The WCF service we use in the background requires a logged in user. Hence i made a guest user with limited rights to solve this issue. So the app steps into the GetAuthenticationStateAsync and tries to fire the AuthenticationStateEvent directly after startup (during a loading screen).</p>
  
  <p><strong><em>EDIT 2</em></strong></p>
  
  <p>So I now tried some additional setup steps, from which Microsoft wrote in the Blazor documentation, that they should not be necessary for server-side blazor:</p>
  
  <p>The ConfigureServices now looks like this</p>
  
  <pre><code>//Authentication &amp; Authorization setup
  services.AddOptions();
  services.AddAuthenticationCore();
  services.AddAuthorizationCore();
  services.AddScoped&lt;IPermissionManager, SingleUserPermissionManager&gt;();
  services.AddScoped&lt;AuthenticationStateProvider, MyAuthenticationStateProvider&gt;();
  services.AddScoped&lt;ServerAuthenticationStateProvider, MyAuthenticationStateProvider&gt;();
  services.AddScoped&lt;IAuthenticationService, MyAuthenticationStateProvider&gt;();
  </code></pre>
  
  <p>In the Configure(IApplicationBuilder app, IWebHostEnvironment env) Method, I added the following calls:</p>
  
  <pre><code>app.UseAuthentication();
  app.UseAuthorization();
  </code></pre>
  
  <p>This did also have no effect.</p>