mz1378
mz1378

Reputation: 2602

How to create an UnAuthenticated Anonymous principal in C#

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);
}

Upvotes: 0

Views: 135

Answers (1)

Qiang Fu
Qiang Fu

Reputation: 8811

var principal = new ClaimsPrincipal(new GenericIdentity("anonymous"));

Upvotes: 0

Related Questions