Hir*_*ndo 2 c# asp.net-identity asp.net-core blazor blazor-server-side
我有一个 .net cor 3.1 Blazor 服务器项目。我在这个项目中使用默认的身份系统。我想在不登录的情况下访问特定的剃须刀页面。我怎么做?任何想法?
我尝试添加@attribute [AllowAnonymous]到剃刀页面。但这对我不起作用
_Host.cshtml
page "/"
@using Microsoft.AspNetCore.Authorization
@namespace has.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@attribute [Authorize]
@{
Layout = null;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>has</title>
<base href="~/" />
<link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
<link href="css/site.css" rel="stylesheet" />
<link href="css/custom_styles.css" rel="stylesheet" />
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700&display=swap" rel="stylesheet" />
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<link href="_content/AntDesign/css/ant-design-blazor.css" rel="stylesheet" />
<link href="_content/Blazor.ContextMenu/blazorContextMenu.min.css" rel="stylesheet" />
<script src="_content/BlazorInputFile/inputfile.js"></script>
<script src="~/js/site.js"></script>
</head>
<body>
<app>
<component type="typeof(App)" render-mode="ServerPrerendered" />
</app>
<div id="blazor-error-ui">
<environment include="Staging,Production">
An error has occurred. This application may no longer respond until reloaded.
</environment>
<environment include="Development">
An unhandled exception has occurred. See browser dev tools for details.
</environment>
<a href="" class="reload">Reload</a>
<a class="dismiss"></a>
</div>
<script src="_framework/blazor.server.js"></script>
<script src="_content/AntDesign/js/ant-design-blazor.js"></script>
<script src="_content/Blazor.ContextMenu/blazorContextMenu.min.js"></script>
<script src="https://kit.fontawesome.com/863157cf0e.js" crossorigin="anonymous"></script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
应用剃刀
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
</Found>
<NotFound>
<LayoutView Layout="@typeof(MainLayout)">
<Error404 />
</LayoutView>
</NotFound>
</Router>
<AntContainer />
</CascadingAuthenticationState>
Run Code Online (Sandbox Code Playgroud)
我找到了答案。@attribute [AllowAnonymous]我在该文件夹内的单独文件夹中创建了另一个 _Host.cshtml Pages,并将我的剃刀页面放入该文件夹中。
添加@layout PublicLayout到您的匿名访问组件。就我而言,是 MyAnonymousComponent.razor。PublicLayout是PublicSection/PublicLayout.razor
我的项目结构是这样的
MyProject
-Pages
-Section1
-Section2
-Shared
_Layout.cshtml
-PublicSection // This folder has anonymous access components
_Host.cshtml
_Layout.cshtml
MyAnonymousComponent.razor
PublicApp.razor
PublicLayout.razor
_Host.cshtml
-Shared
MainLayout.razor
App.razor
_Imports.razor
Program.cs
Startup.cs
Run Code Online (Sandbox Code Playgroud)
MyProject
-Pages
-Section1
-Section2
-Shared
_Layout.cshtml
-PublicSection // This folder has anonymous access components
_Host.cshtml
_Layout.cshtml
MyAnonymousComponent.razor
PublicApp.razor
PublicLayout.razor
_Host.cshtml
-Shared
MainLayout.razor
App.razor
_Imports.razor
Program.cs
Startup.cs
Run Code Online (Sandbox Code Playgroud)
@page "/publicsection"
@using has.Pages.PublicRazor
@using Microsoft.AspNetCore.Authorization
@namespace has.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@attribute [AllowAnonymous]
@{
Layout = null;
}
<!DOCTYPE html>
<html lang="en">
<head>
<base href="~/PublicSection" />
// rest is same as Original _Host.cshtml file
</head>
<body>
<app>
<component type="typeof(PublicApp)" render-mode="ServerPrerendered" />
</app>
// rest is same as Original _Host.cshtml file
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(Program).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(PublicLayout)" />
</Found>
<NotFound>
<LayoutView Layout="@typeof(PublicLayout)">
<Error404 />
</LayoutView>
</NotFound>
</Router>
<AntContainer />
</CascadingAuthenticationState>
Run Code Online (Sandbox Code Playgroud)
@inherits LayoutComponentBase;
<Content Class="site-layout-background content-pan">
@Body
</Content>
@code {
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6115 次 |
| 最近记录: |