How to create an UnAuthenticated Anonymous principal in C#

54 Views Asked by At

I need this to return from AuthenticationStateProvider in blazor web assembly when user is null.

public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
    var principal = new ClaimsPrincipal();
    var user = await _userService.FetchUserFromBrowser();

    if (user is not null)
    {
        var authenticatedUser = await _userService.SendAuthenticateRequestAsync(user.Username, user.Password);

        if (authenticatedUser is not null)
        {
            principal = authenticatedUser.ToClaimsPrincipal();
            CurrentUser = authenticatedUser;
        }

    }
    else
    {
        /*TODO*/
        
    }
    return new(principal);
}
1

There are 1 best solutions below

1
Qiang Fu On
var principal = new ClaimsPrincipal(new GenericIdentity("anonymous"));