我在服务器端渲染中使用了blazor,我想做自己的AuthenticationStateProvider,但这是行不通的,我也不知道为什么。
我在公共类LocalAuthenticationStateProvider:AuthenticationStateProvider 中的ovveride方法:
public async override Task<AuthenticationState> GetAuthenticationStateAsync()
{
if (await _storageService.ContainKeyAsync("User"))
{
var userInfo = await _storageService.GetItemAsync<LocalUserInfo>("User");
var claims = new[]
{
new Claim("Email", userInfo.Email),
new Claim("FirstName", userInfo.FirstName),
new Claim("LastName", userInfo.LastName),
new Claim("AccessToken", userInfo.AccessToken),
new Claim(ClaimTypes.NameIdentifier, userInfo.Id),
};
var identity = new ClaimsIdentity(claims, "BearerToken");
var user = new ClaimsPrincipal(identity);
var state = new AuthenticationState(user);
NotifyAuthenticationStateChanged(Task.FromResult(state));
return state;
}
return new AuthenticationState(new ClaimsPrincipal());
}
我的登录页面:
@inject AuthenticationStateProvider authenticationStateProvider
await storageService.SetItemAsync("User", userInfo);
await authenticationStateProvider.GetAuthenticationStateAsync();
navigationManager.NavigateTo("/");
我的Startup.cs
services.AddAuthentication();
services.AddAuthorization();
services.AddAuthorizationCore();
services.AddScoped<AuthenticationStateProvider, LocalAuthenticationStateProvider>();
我的 Index.razor 用于检查身份验证。这总是未经授权的
<AuthorizeView>
<Authorized>
<h1>Hi! @context.User.FindFirst("FirstName").Value</h1>
</Authorized>
<NotAuthorized>
<RadzenButton Text="login" Click="GoToRegister"></RadzenButton>
</NotAuthorized>
<Authorizing>
<h1>Authentication in progress</h1>
<p>Only visible while authentication is in progress.</p>
</Authorizing>
这是怎么了?
答案 0 :(得分:2)
您应该在登录页面上注入LocalAuthenticationStateProvider
@inject LocalAuthenticationStateProvider LocalAuthStateProvider
这样做之后:
await storageService.SetItemAsync("User", userInfo);
我猜测会存储用户的信息,从而使订户(例如CascadingAuthenticationState
)知道身份验证状态已更改。
这样做是这样的:
LocalAuthStateProvider.NotifyAuthenticationStateChanged();
您的自定义AuthenticationStateProvider应该类似于以下内容:
public async override Task<AuthenticationState> GetAuthenticationStateAsync()
{
ClaimsIdentity identity;
if (await _storageService.ContainKeyAsync("User"))
{
var userInfo = await _storageService.GetItemAsync<LocalUserInfo>
("User");
var claims = new[]
{
new Claim("Email", userInfo.Email),
new Claim("FirstName", userInfo.FirstName),
new Claim("LastName", userInfo.LastName),
new Claim("AccessToken", userInfo.AccessToken),
new Claim(ClaimTypes.NameIdentifier, userInfo.Id),
};
identity = new ClaimsIdentity(claims );
}
else
{
identity = new ClaimsIdentity();
}
return await Task.FromResult(new AuthenticationState(new
ClaimsPrincipal(identity)));
}
public void NotifyAuthenticationStateChanged()
{
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
}
您的Startup类应该是这样的:
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication();
services.AddAuthorization();
// services.AddAuthorizationCore();
services.AddScoped<LocalAuthenticationStateProvider>();
services.AddScoped<AuthenticationStateProvider>(provider => provider.GetRequiredService<LocalAuthenticationStateProvider>());
}
希望这对您有帮助...