若出现此问题,请尝试以下步骤:
下面是一个示例代码,说明如何实现 Blazor Server 中的 AD 组认证:
@page "/auth/ADGroup"
@using Microsoft.AspNetCore.Authorization;
You are authorized!
You are not authorized!
@code {
protected override async Task OnInitializedAsync()
{
await AuthorizeADGroup();
}
private async Task AuthorizeADGroup()
{
var authenticationState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authenticationState.User;
var isInGroup = user.Claims.Any(c => c.Type == "http://schemas.microsoft.com/ws/2008/06/identity/claims/groupsid" && c.Value == "");
if (!isInGroup)
{
NavigationManager.NavigateTo("/auth/signout");
}
}
}
此代码使用了 Blazor 的身份验证封装和策略机制,确保用户被授权访问所需的页面。在该代码中,我们首先在 OnInitializedAsync
方法中调用了 AuthorizeADGroup
方法,以确保用户是否在指定的 AD 组中。
如果用户不在 AD 组中,则导航到应用程序指定的签出 URL。这样可以保证用户只有在成功完成验证和组授权之后才能访问。
请注意,此示例代码仅