如何在 C# 中创建未经身份验证的匿名主体

问题描述 投票:0回答:1

当用户为空时,我需要从 blazor Web 程序集中的

AuthenticationStateProvider
返回此值。

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);
}
c# authentication anonymous
1个回答
0
投票
var principal = new ClaimsPrincipal(new GenericIdentity("anonymous"));
© www.soinside.com 2019 - 2024. All rights reserved.