带有自定义AuthentiCationsCheme的Blazor Server Cookie身份验证

发布于 2025-01-27 17:34:07 字数 1871 浏览 3 评论 0原文

我正在尝试在我的Blazor Server应用程序中构建自定义Cookie身份验证。

只要我使用像这样的defaultauthenticateCheme:

builder.Services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
    options.LoginPath = "/login";
    options.LogoutPath = "/logout";
});

呼叫httpcontext.SignInAsync(cookieAuthenticationDefaults.authenticationschemechemecheme,new Simplessprincipal(索赔Sistentity),authProperties),authproperties;将log me log me。

但是我想使用自定义身份验证化学以下多个方案,例如:

builder.Services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie("Attendee", options =>
{
    options.LoginPath = "/login";
    options.LogoutPath = "/logout";
}).AddCookie("Admin", options =>
{
    options.LoginPath = "/admin/login";
    options.LogoutPath = "/admin/logout";
});

调用httpContext.SignInAsync(“ admin”,new Simplessprincipal(soipssidentity),authproperties),authproperties);还是设置cookie,但仍然设置了cookie,但仍在我的应用告诉我我没有授权。

<AuthorizeView>
    <Authorized>Logged in!</Authorized>
    <NotAuthorized>NOT logged in!</NotAuthorized> <!-- This is shown -->
</AuthorizeView>

我希望能够通过@AtTribute [授权(AuthenTicationsChemes =“ admin”)]@attribute [euthorize(roles =“ admin”)]在每个组件上。

我会想念什么?

I'm trying to build custom cookie authentication in my Blazor Server app.

It works as long as I use the DefaultAuthenticateScheme like this:

builder.Services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
    options.LoginPath = "/login";
    options.LogoutPath = "/logout";
});

Calling HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties); will log me.

But I'd like to use custom AuthenticationSchemes to be able to have multiple schemes like:

builder.Services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie("Attendee", options =>
{
    options.LoginPath = "/login";
    options.LogoutPath = "/logout";
}).AddCookie("Admin", options =>
{
    options.LoginPath = "/admin/login";
    options.LogoutPath = "/admin/logout";
});

Calling HttpContext.SignInAsync("Admin", new ClaimsPrincipal(claimsIdentity), authProperties); do set the cookie, but still my app tells me that I'm not authorized.

<AuthorizeView>
    <Authorized>Logged in!</Authorized>
    <NotAuthorized>NOT logged in!</NotAuthorized> <!-- This is shown -->
</AuthorizeView>

I'd like to be able to control the access with @attribute [Authorize(AuthenticationSchemes = "Admin")] or @attribute [Authorize(Roles = "Admin")] on each component.

What could I be missing?

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

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

发布评论

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

评论(2

同尘 2025-02-03 17:34:07

您的其他cookie没有用于身份验证。

您可以编写自己的授权处理程序/中间件来做到这一点,但是默认情况下,我认为您只能使用一个cookie,并且在代码的这一行中设置了它的名称。

options.defaultauthenticatescheme = cookieAthenticationdefaults.authenticationscheme;

因此,简而言之,它说您没有授权,因为它正在测试该方案cookieAuthenticationDefaults.authenticationschemes.authenticationschemes.cheme,而不是其他两个cookie

Your additional cookies aren't being used for authentication.

You could write your own authorization handler/middleware to do that, but by default, I think you can only use one cookie and you set it's name in this line of your code.

options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;

So, in brief, it's saying you're not authorized because it's testing the scheme CookieAuthenticationDefaults.AuthenticationScheme and not one of your additional two cookies.

满身野味 2025-02-03 17:34:07

我知道这对您来说有点晚了,但这是另一个答案的链接,表明您可以通过使用控制器来帮助管理多个方案的路由来完成您寻找的事情。 链接

I know it's a bit late for you, but here is a link to another answer that indicates you can do what you are looking for by using a controller to help manage the routing for the multiple schemes. link

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