如果未经认证的用户

发布于 2025-02-08 03:48:36 字数 295 浏览 1 评论 0原文

我正在尝试定义一个完全导航的大风网站,以呈现非认证用户的应用程序,并为身份验证的用户提供另一个完全不同的导航。

这个想法是,未经认可的用户可以使用完整的页面和导航的匿名用户进入网站的一部分。一旦登录,他们就可以进入网站的另一部分,该部分仅适用于具有自己导航的经过身份验证的用户。

此外,我可能会根据用户的角色有两组不同的导航。

例如,管理员可能具有其他视图和菜单,而不是标准用户。

我已经查看了路由和导航文档,但找不到任何可以让我根据用户的身份验证状态或角色重定向到具有自己的Mainlayout的其他子路线的任何东西。

I'm trying to define a Blazor website that has complete navigation to present the application for non-authenticated users and another completely different navigation for authenticated users.

The idea is that non-authenticated users get to a part of the website that allows anonymous users, with a complete set of pages and navigation. Once they log in, they get to another part of the website which is only for authenticated users with its own navigation.

In addition, I might have two different sets of navigation based on the roles of the users.

For example, an admin might have other views and menus than a standard user.

I have looked at the Routing and navigation documentation, but can't find anything that would allow me to redirect the user based on its authentication state or role to another sub-route with its own MainLayout, etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无语# 2025-02-15 03:48:36
  • app.razor:

     < cascadingauthenticationState>
        < router appAssembly =“@typeof(program).sembly”>
          < conding context =“ routedata”>
              < welerizerOuteview routedata =“@routedata” 
                  defaultlayout =“@typeof(mainlayout)” />
          </cound>
          < notfound>
              < layoutview layout =“@typeof(mainlayout)”>
                  < p>对不起,这个地址没有什么。</p>
              </layoutview>
          </notfound>
       </router>
    </cascadingauthenticationState>
     
  • navmenu.razor

     < euthorizeView>
       <授权>
          @ *对于授权用户 *@
          < nav> ...</nav>
       </授权>
       < notAthorized>
          @ *对于未经授权的用户 *@
          < nav> ...</nav>
       </notAthorized>
    </oferuizizeview>
     
  • 别忘了添加启动或编程文件这两行。

      app.useuthentication();
    app.useauthorization();
     

Microsoft doc

  • App.razor:

      <CascadingAuthenticationState>
        <Router AppAssembly="@typeof(Program).Assembly">
          <Found Context="routeData">
              <AuthorizeRouteView RouteData="@routeData" 
                  DefaultLayout="@typeof(MainLayout)" />
          </Found>
          <NotFound>
              <LayoutView Layout="@typeof(MainLayout)">
                  <p>Sorry, there's nothing at this address.</p>
              </LayoutView>
          </NotFound>
       </Router>
    </CascadingAuthenticationState>
    
  • NavMenu.razor

    <AuthorizeView>
       <Authorized>
          @* For authorized users *@
          <nav>...</nav>
       </Authorized>
       <NotAuthorized>
          @* For unauthorized users *@
          <nav>...</nav>
       </NotAuthorized>
    </AuthorizeView>
    
  • Don't forget to add in Startup or program file this two lines.

    app.UseAuthentication();
    app.UseAuthorization();
    

Microsoft doc

缱倦旧时光 2025-02-15 03:48:36

查看此代码:

    [Inject]
    public NavigationManager Navigator { get; set; }

    [Inject]
    private IHttpContextAccessor HttpContextAccessor { get; set; }

    public void DoSomething()
    {
        if (HttpContextAccessor.HttpContext.User.Identity.IsAuthenticated)
        {
            Navigator.NavigateTo("/SecretPage");
        }
        else
        {
            Navigator.NavigateTo("/PublicPage");
        }
    }

不要忘记将其添加到您的启动文件中:
services.addhttpcontextaccessor();
这将确保可以注入IHTTPContextAccessor。

NaviationManager无需手动注入。

Take a look at this code:

    [Inject]
    public NavigationManager Navigator { get; set; }

    [Inject]
    private IHttpContextAccessor HttpContextAccessor { get; set; }

    public void DoSomething()
    {
        if (HttpContextAccessor.HttpContext.User.Identity.IsAuthenticated)
        {
            Navigator.NavigateTo("/SecretPage");
        }
        else
        {
            Navigator.NavigateTo("/PublicPage");
        }
    }

Don't forget to add this to your startup file:
services.AddHttpContextAccessor();
That will make sure IHttpContextAccessor can be injected.

NaviationManager does not need to be manually injected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文