在Blazor应用程序中,我们将覆盖@Transactional
的默认实现,以允许使用out Jwt。
自升级到AuthenticationStateProvider
以来,现在需要将Preview 9
响应包装到AthenticationResponse
中。
我有以下代码;
Task
但是我在public class JwtAuthenticationStateProvider : AuthenticationStateProvider
{
private bool _isUserLoggedIn = false;
private readonly HttpClient _httpClient;
private readonly IAuthService _authService;
private readonly ILogger<JwtAuthenticationStateProvider> _logger;
public JwtAuthenticationStateProvider(HttpClient httpClient, IAuthService authService, ILogger<JwtAuthenticationStateProvider> logger)
{
_httpClient = httpClient;
_authService = authService;
_logger = logger;
}
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
if(!_isUserLoggedIn)
{
return await Task.FromResult(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity())));
}
else
{
var tokenResponse = await _authService.GetCurrentAuthTokenAsync();
if (tokenResponse.HasError)
{
var anonymousUser = new ClaimsPrincipal(new ClaimsIdentity());
return await Task.FromResult(new AuthenticationState(anonymousUser));
}
var claimsResponse = await _authService.GetCurrentUserClaimsAsync();
if(claimsResponse.HasError)
{
var anonymousUser = new ClaimsPrincipal(new ClaimsIdentity());
return await Task.FromResult(new AuthenticationState(anonymousUser));
}
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", tokenResponse.Result);
return await Task.FromResult(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(claimsResponse.Result, "apiAuth"))));
}
}
public async Task MarkUserAsAuthenticated()
{
if(!_isUserLoggedIn)
_ = KeepSessionAsync();
var claimsResponse = await _authService.GetCurrentUserClaimsAsync();
var authenticatedUser = new ClaimsPrincipal(new ClaimsIdentity(claimsResponse.Result, "apiAuth"));
NotifyAuthenticationStateChanged(await Task.FromResult(new AuthenticationState(authenticatedUser)));
}
public void MarkUserAsLoggedOut()
{
_isUserLoggedIn = false;
_httpClient.DefaultRequestHeaders.Authorization = null;
var anonymousUser = new ClaimsPrincipal(new ClaimsIdentity());
NotifyAuthenticationStateChanged(Task.FromResult(new AuthenticationState(anonymousUser)));
}
}
上遇到了以下错误;
'JwtAuthenticationStateProvider.GetAuthenticationStateAsync()':返回类型必须为'Task'才能匹配重写的成员'AuthenticationStateProvider.GetAuthenticationStateAsync()'
有人可以解释一下这里发生了什么吗?
答案 0 :(得分:0)
异步方法返回任务:
如果返回:return await Task.FromResult(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(claimsResponse.Result, "apiAuth"))));
,则返回Task<Task<AuthenticationState>>
只返回:return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(claimsResponse.Result, "apiAuth")));
您的代码应为:
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
if(!_isUserLoggedIn)
{
return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity()));
}
else
{
var tokenResponse = await _authService.GetCurrentAuthTokenAsync();
if (tokenResponse.HasError)
{
var anonymousUser = new ClaimsPrincipal(new ClaimsIdentity());
return new AuthenticationState(anonymousUser);
}
var claimsResponse = await _authService.GetCurrentUserClaimsAsync();
if(claimsResponse.HasError)
{
var anonymousUser = new ClaimsPrincipal(new ClaimsIdentity());
return new AuthenticationState(anonymousUser);
}
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", tokenResponse.Result);
return new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(claimsResponse.Result, "apiAuth")));
}
}