如何显示用户身份验证的,但在Blazor的NAV菜单中未授权

发布于 2025-02-07 04:27:09 字数 1188 浏览 2 评论 0原文

我正在开发新兴的新应用程序。我有ASP.NET身份设置和工作。

有没有办法进行身份验证,但没有授权?

例如,最初我们要求用户注册,但直到我们手动添加它们之前,我们才添加其角色。

因此,可以通过输入正确的用户名和密码来验证用户,但没有角色,因此不会有菜单项。

有没有办法在用ASP.NET身份的Blazor Razor中进行此操作?

<AuthorizeView Roles="@X.Shared.Constants.AspNetRolesConstants.Administrator.ToString()">
    <Authorized>
        <h3>Welcome to X Research!</h3>
    </Authorized>
    <NotAuthorized>
        <h3>Welcome to X Research!</h3>
        <br />
        Please Log in!
        <br /><br />
        (If you are returned to this page, please contact an Administrator to have roles added for your account!)
    </NotAuthorized>
</AuthorizeView>

我读了这但这似乎并没有做我想做的事情。

谢谢。

更新

当我使用上述授权>等。然后,当您未登录时,我会看到:

”主索引上的“未授权”。RAZOR页面如果需要登录。

也许有一种方法可以重定向到我的登录页面,如果它们未获得授权?

I'm working on a new application in Blazor. I have asp.net identity setup and working.

Is there a way to be authenticated, but not authorized?

For example, initially we are requiring users to register, but are not adding their roles until we manually add them.

So the user could be authenticated by entering the correct user name and password, but has no roles, so there would be no menu items.

Is there a way to do this in Blazor razor with asp.net identity?

<AuthorizeView Roles="@X.Shared.Constants.AspNetRolesConstants.Administrator.ToString()">
    <Authorized>
        <h3>Welcome to X Research!</h3>
    </Authorized>
    <NotAuthorized>
        <h3>Welcome to X Research!</h3>
        <br />
        Please Log in!
        <br /><br />
        (If you are returned to this page, please contact an Administrator to have roles added for your account!)
    </NotAuthorized>
</AuthorizeView>

I read this article, but it doesn't seem to do what I want to do.

Thanks.

Update

When I use the above AuthorizeView etc., then when you are not logged in, I see this:

enter image description here

So I would rather not show 'Not Authorized' on the main Index.razor page if they need to log in.

Maybe there is a way to redirect to my Login page if they are not authenicated?

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

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

发布评论

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

评论(2

灼痛 2025-02-14 04:27:09

是的,您可以重定向到其他页面,只是注入NavigationManager:

@inject NavigationManager NavManager

<h1>Redirect Page</h1>

<p>Redirecting to Default Page</p>

@code {
    protected override void OnInitialized()
    {
        NavManager.NavigateTo("/");
    }
}

Yes you can redirect to other Pages just inject the NavigationManager:

@inject NavigationManager NavManager

<h1>Redirect Page</h1>

<p>Redirecting to Default Page</p>

@code {
    protected override void OnInitialized()
    {
        NavManager.NavigateTo("/");
    }
}
一身仙ぐ女味 2025-02-14 04:27:09

@david让我做正确的答案:

钥匙是添加@Attribute [allyAnymous],因此它允许经过身份验证的构建。如果我不说,它将说“未授权”

这是我的完整代码:

@page "/"
@inject NavigationManager navigationManager;
@attribute [AllowAnonymous]

 <h3>Welcome to Research!</h3>

@code{

    [CascadingParameter] protected Task<AuthenticationState> AuthStat { get; set; }

    protected async override Task OnInitializedAsync()
    {
        base.OnInitialized();
        var user = (await AuthStat).User;

        if (user.Identity == null)
        {
            throw new GenericException("user is null");
        }

        if(!user.Identity.IsAuthenticated)
        {
            navigationManager.NavigateTo("/Identity/Account/Login", true);
        }
    }
}

@David let me do the correct answer:

The key was adding @attribute [AllowAnonymous] so it allows past the build in authentication. If I do not put this, it will say 'Not Authorized'

Here is my full code:

@page "/"
@inject NavigationManager navigationManager;
@attribute [AllowAnonymous]

 <h3>Welcome to Research!</h3>

@code{

    [CascadingParameter] protected Task<AuthenticationState> AuthStat { get; set; }

    protected async override Task OnInitializedAsync()
    {
        base.OnInitialized();
        var user = (await AuthStat).User;

        if (user.Identity == null)
        {
            throw new GenericException("user is null");
        }

        if(!user.Identity.IsAuthenticated)
        {
            navigationManager.NavigateTo("/Identity/Account/Login", true);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文