如何在 Blazor Server .Net 6 中默认要求对所有人进行身份验证(自动登录页面重定向)
How to require authentication for all by default in Blazor Server .Net 6 (with automatic login Page redirect)
我已经尝试过为以上 .Net 3 制作的这个但它不起作用:
builder.Services.AddAuthorization(options =>
{
options.FallbackPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
});
抱歉,我还应该提到,如果用户未通过身份验证,我想自动重定向到登录页面。
@attribute [Authorize]
只会通过说“未授权”来阻止访问,但我希望用户被重定向到登录页面
在 _Imports.razor 文件中添加 @attribute [Authorize]
后,这就是显示的内容
enter image description here
只需在 _Import.razor 文件中添加 @attribute [Authorize]
。
此时,如果需要一些无需登录的页面,添加@attribute [AllowAnonymous]
我相信这会奏效...将以下代码片段放入 _Imports.razor 文件
@using Microsoft.AspNetCore.Authorization
@attribute [Authorize]
在这种情况下,当点击索引页面时,用户将被重定向到登录页面。如果要在呈现 Blazor 应用程序之前执行身份验证,请在 _Host.cshtml 文件
中添加上面的代码片段
将 @attribute [AllowAnonymous]
添加到要从身份验证中排除的特定页面,例如索引页面。
更新:下面描述了如何在用户未通过身份验证时强制自动显示登录页面:
扩展 AuthorizeRouteView
组件,像这样驻留在 App 组件中:
<AuthorizeRouteView RouteData="@routeData"
DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
@{
NavigationManager.NavigateTo("identity/account/login",
forceLoad: true);
}
</NotAuthorized>
<Authorizing>
Wait...
</Authorizing>
</AuthorizeRouteView>
如果用户无权查看页面,上面的代码会将用户导航到登录页面。警告用户可能已通过身份验证但仍未被授权查看特定页面。您的代码应该验证用户是否已通过身份验证,如果他通过了身份验证,请让他知道他无权查看当前页面。如果他未通过身份验证,请将他发送到登录页面。请注意,基本授权只需要对用户进行身份验证。
下面是 App.razor 组件的完整代码:
@inject NavigationManager NavigationManager
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly" PreferExactMatches="@true">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData"
DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
@{
NavigationManager.NavigateTo("identity/account/login",
forceLoad: true);
}
</NotAuthorized>
<Authorizing>
Wait...
</Authorizing>
</AuthorizeRouteView>
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>
我已经尝试过为以上 .Net 3 制作的这个但它不起作用:
builder.Services.AddAuthorization(options =>
{
options.FallbackPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
});
抱歉,我还应该提到,如果用户未通过身份验证,我想自动重定向到登录页面。
@attribute [Authorize]
只会通过说“未授权”来阻止访问,但我希望用户被重定向到登录页面
在 _Imports.razor 文件中添加 @attribute [Authorize]
后,这就是显示的内容
enter image description here
只需在 _Import.razor 文件中添加 @attribute [Authorize]
。
此时,如果需要一些无需登录的页面,添加@attribute [AllowAnonymous]
我相信这会奏效...将以下代码片段放入 _Imports.razor 文件
@using Microsoft.AspNetCore.Authorization
@attribute [Authorize]
在这种情况下,当点击索引页面时,用户将被重定向到登录页面。如果要在呈现 Blazor 应用程序之前执行身份验证,请在 _Host.cshtml 文件
中添加上面的代码片段将 @attribute [AllowAnonymous]
添加到要从身份验证中排除的特定页面,例如索引页面。
更新:下面描述了如何在用户未通过身份验证时强制自动显示登录页面:
扩展 AuthorizeRouteView
组件,像这样驻留在 App 组件中:
<AuthorizeRouteView RouteData="@routeData"
DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
@{
NavigationManager.NavigateTo("identity/account/login",
forceLoad: true);
}
</NotAuthorized>
<Authorizing>
Wait...
</Authorizing>
</AuthorizeRouteView>
如果用户无权查看页面,上面的代码会将用户导航到登录页面。警告用户可能已通过身份验证但仍未被授权查看特定页面。您的代码应该验证用户是否已通过身份验证,如果他通过了身份验证,请让他知道他无权查看当前页面。如果他未通过身份验证,请将他发送到登录页面。请注意,基本授权只需要对用户进行身份验证。
下面是 App.razor 组件的完整代码:
@inject NavigationManager NavigationManager
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly" PreferExactMatches="@true">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData"
DefaultLayout="@typeof(MainLayout)">
<NotAuthorized>
@{
NavigationManager.NavigateTo("identity/account/login",
forceLoad: true);
}
</NotAuthorized>
<Authorizing>
Wait...
</Authorizing>
</AuthorizeRouteView>
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>