带有自定义AuthentiCationsCheme的Blazor Server Cookie身份验证
我正在尝试在我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的其他cookie没有用于身份验证。
您可以编写自己的授权处理程序/中间件来做到这一点,但是默认情况下,我认为您只能使用一个cookie,并且在代码的这一行中设置了它的名称。
options.defaultauthenticatescheme = cookieAthenticationdefaults.authenticationscheme;
因此,简而言之,它说您没有授权,因为它正在测试该方案
cookieAuthenticationDefaults.authenticationschemes.authenticationschemes.cheme
,而不是其他两个cookieYour 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.我知道这对您来说有点晚了,但这是另一个答案的链接,表明您可以通过使用控制器来帮助管理多个方案的路由来完成您寻找的事情。 链接
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