Authentication Blazor-在不同布局之间使用级联身份验证状态
我有一个.NET Core 3.1 Blazor项目,其中应用程序中有3个子文件夹,每个文件夹提供一个页面,每个页面的内容将使用Three.JS由WebGL驱动 在我测试的第一个文件夹中,我创建了两个项目。第一个名为_TestPage.cshtml,另一个名为Index.razor。两个页面中都包含以下@page指令:Authentication Blazor-在不同布局之间使用级联身份验证状态,authentication,razor,layout,blazor,Authentication,Razor,Layout,Blazor,我有一个.NET Core 3.1 Blazor项目,其中应用程序中有3个子文件夹,每个文件夹提供一个页面,每个页面的内容将使用Three.JS由WebGL驱动 在我测试的第一个文件夹中,我创建了两个项目。第一个名为_TestPage.cshtml,另一个名为Index.razor。两个页面中都包含以下@page指令: @page "/Applications/TestFolder1/Index" _TestPage.cshtml页面包含以下内容: <!DOCTYPE html>
@page "/Applications/TestFolder1/Index"
_TestPage.cshtml页面包含以下内容:
<!DOCTYPE html>
<html lang="en">
<body>
<app>
<component type="typeof(App)" render-mode="Server" />
</app>
</body>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>My Blazor App</title>
<base href="/" />
<script src="_framework/blazor.server.js"></script>
<style type="text/css">
body {
background-color: red;
}
</style>
</head>
</html>
@layout EmptyLayout
@page "/Applications/Monitor/Index"
<AuthorizeView>
<Authorized>
<p style="color: black;">***I can not see this text either...***</p>
</Authorized>
<NotAuthorized>
<RedirectNotAuthorizedAccess />
</NotAuthorized>
</AuthorizeView>
Index.razor页面中还包含以下内容:
<!DOCTYPE html>
<html lang="en">
<body>
<app>
<component type="typeof(App)" render-mode="Server" />
</app>
</body>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>My Blazor App</title>
<base href="/" />
<script src="_framework/blazor.server.js"></script>
<style type="text/css">
body {
background-color: red;
}
</style>
</head>
</html>
@layout EmptyLayout
@page "/Applications/Monitor/Index"
<AuthorizeView>
<Authorized>
<p style="color: black;">***I can not see this text either...***</p>
</Authorized>
<NotAuthorized>
<RedirectNotAuthorizedAccess />
</NotAuthorized>
</AuthorizeView>
但是,当应用程序启动时,我可以直接转到子文件夹中的索引页,而无需登录。我得到一个坚实的红色背景,但没有看到任何渲染文本,也没有重定向到登录页面,因为我没有登录
当我没有使用Visual Studio创建的默认MainLayout组件,并且使用@layout指令在razor页面中重写了布局时,是否需要执行一些不同的操作以使级联身份验证状态工作?我相信我可能已经解决了我的问题。似乎有两件事情设置不正确。首先,我的EmptyLayout组件中没有定义AuthroizeView组件网来处理未经身份验证的用户。第二个问题是,当使用NavigationManager重定向到登录页面时,我的RedirectNotAuthorizedAccess组件没有执行forceLoad=true。这会阻止登录页面显示,并且仍然显示原始帖子中我的示例中nest CSTHML布局的纯红背景 上面CSHTML页面的代码保持不变,现在我的另外两个组件的代码如下所示: 剃刀索引
@page "/Application/TestFolder1/Index"
@layout EmptyLayout
@code {
}
剃须刀
@inherits LayoutComponentBase;
<AuthorizeView>
<Authorized>
<p style="color: green;">I can now see this text...</p>
</Authorized>
<NotAuthorized>
<RedirectNotAuthorizedAccess />
</NotAuthorized>
</AuthorizeView>
我以样品的方式解决了这个问题,并为我完美地工作 通过使用JWT或授权用户的任何流程,用于检测授权用户或非授权用户 用于更改用户身份验证或不使用GetAuthenticationStateAncy重写方法 我正在使用JWT检测用户登录成功与否
string token = await TokenAsync();
ClaimsIdentity identity = await IsAuthenticated(token)
? new ClaimsIdentity(new[] { new Claim(ClaimTypes.Name, "mrfibuli"), "Fake authentication type") // true, auth
: new ClaimsIdentity(); // false, no auth
return new AuthenticationState(new ClaimsPrincipal(identity));
你能用“永远真实”吗
然后,您可以为响应身份验证StateProvider服务更改App.razor吗
App.razor
iServicePayroll.Pages.Home.Login我的登录页面