C# Blazor.Net Core 3.0预览9-AuthenticationStateProvider实现问题
在Blazor应用程序中,我们覆盖了C# Blazor.Net Core 3.0预览9-AuthenticationStateProvider实现问题,c#,blazor,.net-core-3.0,blazor-client-side,C#,Blazor,.net Core 3.0,Blazor Client Side,在Blazor应用程序中,我们覆盖了AuthenticationStateProvider的默认实现,以允许使用out Jwt 由于升级到Preview 9,现在需要将AthenticationResponse响应包装到任务中 我有以下代码 public class JwtAuthenticationStateProvider : AuthenticationStateProvider { private bool _isUserLoggedIn = false; privat
AuthenticationStateProvider
的默认实现,以允许使用out Jwt
由于升级到Preview 9
,现在需要将AthenticationResponse
响应包装到任务中
我有以下代码
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:AuthenticationStateProvider
{
private bool_isUserLoggedIn=false;
私有只读HttpClientu HttpClient;
私有只读IAuthService\u authService;
专用只读ILogger\u记录器;
公共JwtAuthenticationStateProvider(HttpClient HttpClient、IAuthService authService、ILogger记录器)
{
_httpClient=httpClient;
_authService=authService;
_记录器=记录器;
}
公共重写异步任务GetAuthenticationStateAync()
{
如果(!\u isUserLoggedIn)
{
返回wait Task.FromResult(新身份验证状态(new-ClaimsPrincipal(new-ClaimsIdentity()));
}
其他的
{
var tokenResponse=wait_authService.GetCurrentAuthTokenAsync();
if(tokenResponse.HasError)
{
var anonymousUser=newclaimsprincipal(newclaimsidentity());
返回wait Task.FromResult(新身份验证状态(匿名用户));
}
var claimsResponse=await_authService.GetCurrentUserClaimsAsync();
if(claimsResponse.HasError)
{
var anonymousUser=newclaimsprincipal(newclaimsidentity());
返回wait Task.FromResult(新身份验证状态(匿名用户));
}
_httpClient.DefaultRequestHeaders.Authorization=新的AuthenticationHeaderValue(“bearer”,tokenResponse.Result);
返回wait Task.FromResult(新的身份验证状态(新的ClaimsPrincipal(新的ClaimsIdentity)(claimsResponse.Result,“apiAuth”));
}
}
公共异步任务MarkUserAsAuthenticated()
{
如果(!\u isUserLoggedIn)
_=KeepSessionAsync();
var claimsResponse=await_authService.GetCurrentUserClaimsAsync();
var authenticatedUser=newclaimsprincipal(newclaimsidentity(claimsResponse.Result,“apiAuth”);
NotifyAuthenticationStateChanged(等待任务.FromResult(新的AuthenticationState(authenticatedUser)));
}
public void MarkUserAsLoggedOut()
{
_isUserLoggedIn=false;
_httpClient.DefaultRequestHeaders.Authorization=null;
var anonymousUser=newclaimsprincipal(newclaimsidentity());
NotifyAuthenticationStateChanged(Task.FromResult(新身份验证状态(匿名用户));
}
}
但是,我在getAuthenticationStateAync()
上遇到以下错误:
“JwtAuthenticationStateProvider.GetAuthenticationStateAync()”:返回类型必须为“Task”,才能与重写的成员“AuthenticationStateProvider.GetAuthenticationStateAync()”匹配
有人能解释一下这里发生了什么吗?异步方法返回任务:
如果返回:return wait Task.FromResult(新身份验证状态(new-ClaimsPrincipal(new-ClaimsIdentity)(claimsResponse.Result,“apiAuth”)))代码>您正在返回一个任务
只需返回:returnnewauthenticationstate(newclaimsprincipal(newclaimsidentity)(claimsResponse.Result,“apiAuth”))代码>
您的代码应该是:
public override async Task getAuthenticationStateAync()
{
如果(!\u isUserLoggedIn)
{
返回新的AuthenticationState(newclaimsprincipal(newclaimsidentity());
}
其他的
{
var tokenResponse=wait_authService.GetCurrentAuthTokenAsync();
if(tokenResponse.HasError)
{
var anonymousUser=newclaimsprincipal(newclaimsidentity());
返回新的AuthenticationState(匿名用户);
}
var claimsResponse=await_authService.GetCurrentUserClaimsAsync();
if(claimsResponse.HasError)
{
var anonymousUser=newclaimsprincipal(newclaimsidentity());
返回新的AuthenticationState(匿名用户);
}
_httpClient.DefaultRequestHeaders.Authorization=新的AuthenticationHeaderValue(“bearer”,tokenResponse.Result);
返回新的身份验证状态(newclaimsprincipal(newclaimsidentity(claimsResponse.Result,“apiAuth”));
}
}
可能您正在混合引用的程序集,并且您使用的任务与基类中的任务不同,例如,不同的程序集实现任务?不是。我不认为我引用的是系统.线程.任务.任务
以及AuthenticationStateProvider
类上的元。如果删除等待
并让返回任务。FromResult(new AuthenticationState)(new ClaimsPrincipal(新的ClaimsEntity(claimsResponse.Result,“apiAuth”);
?@HenkHolterman不,它不是直接从AuthenticationStateProvider扩展而来的。我已经尝试过这一点(因为这是在预览9之前的编码方式)。但是,这会导致错误AuthenticationState不包含GetWaiter的定义,无法找到接受类型为AuthenticationState的第一个参数oups,remove await,它应该可以工作。我更新了响应