授权访问
项目通常会伴随着不同的环境,开发,测试,生产等对日志面板做不同的鉴权访问。LogDashboard提供了授权过滤器让开发者控制授权
Logdashboard预构建了以下两种常用的授权过滤器
LogDashboardRoleAuthorizeFilter
用于进行角色校验,与ASP.NET Core Identity系统集成。LogDashboardBasicAuthFilter
使用简单的 WWW-authenticate 方式进行授权LogDashboardOptions
提供了 AddAuthorizationFilter
方法添加授权过滤器services.AddLogDashboard(opt =>
{
opt.AddAuthorizationFilter(new LogDashboardRoleAuthorizeFilter(new List<string> {"admin"}));
});
在注册服务时注册预构建的角色过滤器,上面的示例代码中,只有具有
admin
角色的用户才可以访问到 logdashboardservices.AddLogDashboard(opt =>
{
opt.AddAuthorizationFilter(new LogDashboardBasicAuthFilter("admin", "123qwe"));
});
在注册服务时注册预构建的BasicAuth过滤器,并设置了访问用户名与密码,这时访问 logdashboard 会显示以下会话框

创建自定义过滤器类
SimpleAuthFilter
继承自 ILogDashboardAuthorizationFilter
接口public class SimpleAuthFilter : ILogDashboardAuthorizationFilter
{
public bool Authorization(LogDashboardContext context)
{
return context.HttpContext.User.Identity.IsAuthenticated;
}
}
将过滤器添加到服务中
services.AddLogDashboard(opt => opt.AddAuthorizationFilter(new SimpleAuthFilter()));
enjoy
Last modified 3yr ago