【发布时间】:2023-01-07 19:33:53
【问题描述】:
我正在关注一篇关于 Blazor WebAssembly 身份验证的文章。
https://code-maze.com/blazor-webassembly-authentication-aspnetcore-identity/
这是 AuthenticationService.cs。
public async Task<AuthResponseDto> Login(UserForAuthenticationDto userForAuthentication)
{
var content = JsonSerializer.Serialize(userForAuthentication);
var bodyContent = new StringContent(content, Encoding.UTF8, "application/json");
var authResult = await _client.PostAsync("accounts/login", bodyContent);
var authContent = await authResult.Content.ReadAsStringAsync();
var result = JsonSerializer.Deserialize<AuthResponseDto>(authContent, _options);
if (!authResult.IsSuccessStatusCode)
return result;
await _localStorage.SetItemAsync("authToken", result.Token);
((AuthStateProvider)_authStateProvider).NotifyUserAuthentication(userForAuthentication.Email);
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("bearer", result.Token);
return new AuthResponseDto { IsAuthSuccessful = true };
}
public async Task Logout()
{
await _localStorage.RemoveItemAsync("authToken");
((AuthStateProvider)_authStateProvider).NotifyUserLogout();
_client.DefaultRequestHeaders.Authorization = null;
}
我在这部分迷路了。
((AuthStateProvider)_authStateProvider).NotifyUserAuthentication(userForAuthentication.Email);
我无法获取此代码。类型铸造?类型转换? 此代码调用方法 NotifyUserAuthentication。但是前面的部分是什么意思呢? 通常,我知道变量前面的 ( ) 是用于转换的。 但是我不明白这是为了什么,这段代码是什么意思?
以及为什么double使用同一个类AuthenticationStateProvider。
AuthStateProvider 继承自 AuthenticationStateProvider。 _authStateProvider 是 AuthenticationStateProvider 的一个实例。
任何帮助都可能对我有帮助。
【问题讨论】:
标签: authentication blazor webassembly