我想完全绕过 AspnetCore 身份,而是使用对当前用户进行身份验证的自定义方法。
但是,我仍然希望现有的授权框架能够发挥作用。换句话说,我希望能够使用AuthorizeView
和@attribute [Authorize]
来维护安全性。
我搜索了又搜索,但没有找到任何关于如何实现这一点的细节。
Blazor 服务器端,.NET Core 3.1.x 查看有关授权的示例,我正在尝试获取自定义授权过滤器/属性的解决方案。我只需要在授权期间检查用户身份。
https://learn.microsoft.com/en-us/aspnet/core/security/blazor/?view=aspnetcore-3.1
在 Blazor 页面顶部,@page 之后
@attribute [MyAuthFilter]
Run Code Online (Sandbox Code Playgroud)
过滤器。然而,OnAuthorization 永远不会受到影响。
public class MyAuthFilter: AuthorizeAttribute,IAuthorizationFilter
{
public void OnAuthorization(AuthorizationFilterContext context)
{
var httpContext = context.HttpContext;
// get user name
string userName = httpContext.User.Identity.Name;
// todo - call method to check user access
// check against list to see if access permitted
//context.Result = new UnauthorizedResult();
}
}
Run Code Online (Sandbox Code Playgroud)