如何在Blazor Server中实现自定义授权
我想完全绕过Aspnetcore身份,而是使用一种对当前用户进行身份验证的自定义方法。
但是,我仍然希望现有的授权框架工作。换句话说,我希望能够使用授权>
和 @attribute [授权]
来维护安全性。
我已经搜索和搜索过,但没有找到有关如何实施此操作的任何详细信息。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,因此您想在Blazor Server应用程序中实现自定义身份验证。换句话说,您想使用与ASP.NET身份不同的方法进行注册和身份验证用户。但是,您仍然需要使用
授权
好品(例如授权视图和[授权]
属性)属性。请注意,我在这里说的仅适用于Blazor Server应用程序。如果您使用的是Glazor WebAssembly,则需要一个不同的解决方案,该解决方案是完全不同的安全模型。
好的,要开始:要实现自定义身份验证,您需要实现一个名为
AuthenticationStateProvider
的类。这是 docs 用于创建自定义AuthenticationStateProvider。但是,文档没有提供完整的解决方案,因此基于文档的其余部分:
在此之后,将其注册在program.cs.cs中,然后评论现有的内容:
现在您可以使用它可以记录您的用户:
在您的实现中,当然,您需要在您认为合适的情况下授权用户。
免责声明:以任何方式我都不是安全专家。除了我吃午餐时,除了短暂的烟雾测试外,以上没有测试。
请注意,这不会使用户关闭页面或刷新页面。
但是,有了以上,内置的授权框架起作用,包括角色。
Ok, so you want to implement Custom Authentication in your Blazor Server app. In other words, you want to use a different method than ASP.net Identity to register and authenticate users. But you still want to use the built in
Authorization
goodies such as AuthorizedView and the[Authorize]
attribute on your pages.Note that what I say here is only applicable to a Blazor Server App. If you are using a Blazor Webassembly, you need a different solution which is not covered here, as it is a completely different security model.
Ok, to get started: To implement Custom Authentication, you need to implement a class called
AuthenticationStateProvider
.Here is the link to the docs for creating a Custom AuthenticationStateProvider. However, the docs don't give a complete solution, so here is the rest of it based on the docs:
After this is in place, register it in program.cs, and comment out the existing one:
Now you are ready to use it to log your user in:
In your implementation, of course you need to authorize your user as you see fit.
Disclaimer: I am not a security expert in any way. The above has not been tested other than a brief smoke test while I had lunch.
Note that this does not keep the user closes the page, or refreshes the page.
However, with the above in place, the built in Authorization framework works, including roles.
确实,所有信息在文档中都非常分散。
这就是为什么我创建此示例的原因。
https://github.com/iso88859/aspnetcoreauthmultilang
在文件的最后,请使用myServerauterauthectateprication cantateprication cantateprication cantateprication cantateprication cantateprication cantateProvider。角色。
Indeed, all the information is quite fragmented in the documentation.
That's why I created this example.
https://github.com/iso8859/AspNetCoreAuthMultiLang
At the end of file myServerAuthenticationStateProvider.cs you can play with roles.