AuthorizationHandler gets new instance of injected service

I have a Blazor app with custom AuthorizationHandler and IAuthorizationRequirement. Snippet from the AuthorizationHandler:

    internal class PermissionLevelAuthorizationHandler : AuthorizationHandler<PermissionLevelRequirement>
    {
        private readonly AuthService _authService;


        public PermissionLevelAuthorizationHandler(AuthService authService)
        {
            _authService= authService;
        }

        protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, PermissionLevelRequirement requirement)
        {
            string? requiredResource = ((DefaultHttpContext?)context.Resource)?.Request.Path;
            int requiredLevel = requirement.Level;
            var canAccess = _authService.GetPermission(requiredResource, context.User, requiredLevel);
            if (canAccess) {
                context.Succeed(requirement);
            }
        }
}

AuthorizationHandler has injected AuthService, which does two things:

  1. Fetches from database all the groups that current user belongs to

  2. Checks (again in database) if any of the user’s groups can access the required page with required permission level.

Both AuthService and AuthorizationHandler are scoped services.

Since the AuthService has to do two queries for each authorization check, I wanted to store the current user permissions in the AuthService and only contact the database if I don’t have the permissions loaded yet.

Problem: The problem is that it seems that AuthorizationHandler gets new instance of AuthService every time even though they are both scoped. Therefore I can’t store any data in the service. Why is that and is there any way to fix that? Are there any better ways of doing that?

As far as I know the alternatives are:

  • store claims in the authentication cookie. That would reduce my service from doing 2 queries to doing only the second one. But the problem is, user can have many groups so the cookie can become too big.
  • store in browser’s local storage or session storage. I would prefer not to store permissions there.
  • store permissions for all users in a singleton service and refresh them all periodically. That would work but I don’t like the fact that the user would have to wait for the refresh after we change permissions.

Also, the authentication is done by a central app. So the authentication cookie is shared amongst multiple apps. Which makes storage a bit more complicated because I would have to handle deletion of data correctly on all apps. That’s why it seems the best solution is to store data in DI service instance.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật