除了 blazor 中的当前页面,我无法从其他页面访问 protectedSessionStore
I cannot access protectedSessionStore from other pages except current page in blazor
如果我不离开我登录的页面,无论如何我都会提交会话。但是,尽管我在同一个浏览器中打开了一个新页面,但我无法访问它。为什么会这样,知道吗?
关键代码片段:
Startup.cs > 配置服务:
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddAuthorization();
services.AddAuthentication();
services.AddScoped<AuthenticationStateProvider, UserService>();
services.AddHttpContextAccessor();
services.AddHttpClient();
用户服务:
public async Task<UserDto> GetUserSession()
{
var localUserJson = await protectedSessionStore.GetAsync<string>(USER_SESSION_OBJECT_KEY);
}
login.razor:
protected override async void OnAfterRender(bool firstRender)
{
if (firstRender)
{
if (await ServiceProvider.Get<UserService>().GetUserSession() != null)
{
NavigationManager.NavigateTo("/Dashboard");
}
}
}
Although I open a new page in the same browser, I can not access it.
这是预期的行为。
根据 the docs:
- sessionStorage is scoped to the browser tab. If the user reloads the tab, the state persists. If the user closes the tab or the browser, the state is lost. If the user opens multiple browser tabs, each tab has its own independent version of the data.
听起来你想要 LocalStorage:
- localStorage is scoped to the browser's window. If the user reloads the page or closes and re-opens the browser, the state persists. If the user opens multiple browser tabs, the state is shared across the tabs. Data persists in localStorage until explicitly cleared.
如果我不离开我登录的页面,无论如何我都会提交会话。但是,尽管我在同一个浏览器中打开了一个新页面,但我无法访问它。为什么会这样,知道吗?
关键代码片段:
Startup.cs > 配置服务:
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddAuthorization();
services.AddAuthentication();
services.AddScoped<AuthenticationStateProvider, UserService>();
services.AddHttpContextAccessor();
services.AddHttpClient();
用户服务:
public async Task<UserDto> GetUserSession()
{
var localUserJson = await protectedSessionStore.GetAsync<string>(USER_SESSION_OBJECT_KEY);
}
login.razor:
protected override async void OnAfterRender(bool firstRender)
{
if (firstRender)
{
if (await ServiceProvider.Get<UserService>().GetUserSession() != null)
{
NavigationManager.NavigateTo("/Dashboard");
}
}
}
Although I open a new page in the same browser, I can not access it.
这是预期的行为。
根据 the docs:
- sessionStorage is scoped to the browser tab. If the user reloads the tab, the state persists. If the user closes the tab or the browser, the state is lost. If the user opens multiple browser tabs, each tab has its own independent version of the data.
听起来你想要 LocalStorage:
- localStorage is scoped to the browser's window. If the user reloads the page or closes and re-opens the browser, the state persists. If the user opens multiple browser tabs, the state is shared across the tabs. Data persists in localStorage until explicitly cleared.