多个 Blazor 服务器应用程序和 Azure 身份验证注销问题
Multiple Blazor Server Apps and Azure Authentication Logout Issue
我有 2 个 Blazor 服务器端应用程序,每个应用程序在 Azure AD 中都有自己的应用程序注册。在 VS2019、.NET Core 3.0 中创建新的 Blazor 服务器端应用程序几乎是开箱即用的。
当它们在浏览器的 2 个选项卡中打开并且我退出应用程序 A 时,我仍然能够导航应用程序 B 中的页面链接。应用程序 B 看不到我已经退出,直到我按下浏览器的页面刷新按钮。
我试过在页面中添加@attribute [Authorize],在App.razor页面定义<NotAuthorized>
,在OnInitializedAsync
页面使用AuthenticationStateProvider
函数检查是否 .IsAuthenticated
没有运气。
@page "/fetchdata"
@attribute [Authorize]
[CascadingParameter] Task<AuthenticationState> authenticationStateTask { get; set; }
protected override async Task OnInitializedAsync()
{
var authState = await authenticationStateTask;
var user = authState.User;
if (!user.Identity.IsAuthenticated)
{
NavigationManager.NavigateTo("Error");
}
}
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
<h1>Please Log In.</h1>
</NotAuthorized>
</AuthorizeRouteView>
</Found>
<NotFound>
<CascadingAuthenticationState>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</CascadingAuthenticationState>
</NotFound>
</Router>
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(AzureADDefaults.AuthenticationScheme).AddAzureAD(options => Configuration.Bind("AzureAd", options));
services.AddControllersWithViews(options =>
{
var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
options.Filters.Add(new AuthorizeFilter(policy));
});
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddSingleton<WeatherForecastService>();
services.AddServerSideBlazor().AddCircuitOptions(options => { options.DetailedErrors = true; });
services.AddSingleton<TestContextService>();
}
这可能与 Chrome 和 Firefox 等 Web 浏览器对其 SameSite for cookie 的实现进行了重大更改有关。
您可以尝试此处描述的缓解步骤:
https://github.com/aspnet/AspNetCore/issues/14996
我有 2 个 Blazor 服务器端应用程序,每个应用程序在 Azure AD 中都有自己的应用程序注册。在 VS2019、.NET Core 3.0 中创建新的 Blazor 服务器端应用程序几乎是开箱即用的。
当它们在浏览器的 2 个选项卡中打开并且我退出应用程序 A 时,我仍然能够导航应用程序 B 中的页面链接。应用程序 B 看不到我已经退出,直到我按下浏览器的页面刷新按钮。
我试过在页面中添加@attribute [Authorize],在App.razor页面定义<NotAuthorized>
,在OnInitializedAsync
页面使用AuthenticationStateProvider
函数检查是否 .IsAuthenticated
没有运气。
@page "/fetchdata"
@attribute [Authorize]
[CascadingParameter] Task<AuthenticationState> authenticationStateTask { get; set; }
protected override async Task OnInitializedAsync()
{
var authState = await authenticationStateTask;
var user = authState.User;
if (!user.Identity.IsAuthenticated)
{
NavigationManager.NavigateTo("Error");
}
}
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
<h1>Please Log In.</h1>
</NotAuthorized>
</AuthorizeRouteView>
</Found>
<NotFound>
<CascadingAuthenticationState>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</CascadingAuthenticationState>
</NotFound>
</Router>
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(AzureADDefaults.AuthenticationScheme).AddAzureAD(options => Configuration.Bind("AzureAd", options));
services.AddControllersWithViews(options =>
{
var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
options.Filters.Add(new AuthorizeFilter(policy));
});
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddSingleton<WeatherForecastService>();
services.AddServerSideBlazor().AddCircuitOptions(options => { options.DetailedErrors = true; });
services.AddSingleton<TestContextService>();
}
这可能与 Chrome 和 Firefox 等 Web 浏览器对其 SameSite for cookie 的实现进行了重大更改有关。
您可以尝试此处描述的缓解步骤: https://github.com/aspnet/AspNetCore/issues/14996