Different user categories frequently use the same backend service in microservices applications:
- Administrators and support personnel in the back office
- Drivers and fleet managers that utilize the Transporter Panel
Only Transporter users should be able to access some APIs for security reasons, and internal logic could need the UserId and CompanyId from the JWT token.
This article demonstrates:
- Method-level endpoint security for Transporter users
- JWT authentication with claims
- Attribute-based policies in ASP.NET Core
- Accessing
UserIdandCompanyIdin controllers
JWT Token Structure
For Transporter users, the JWT might contain claims like:
UserTypedifferentiates roles (TransportervsBackOffice)CompanyIdidentifies the organizationUserIduniquely identifies the user
Step 1: Create TransporterWithCompany Attribute
We create a custom attribute to restrict access to Transporter users with a company:
This attribute can be applied per method, leaving other endpoints unrestricted.
Step 2: Configure JWT Authentication and Authorization
appsettings.json example:
Step 3: Claims Helper
Step 4: Middleware to Inject User Context
Step 5: Sample Controller Using Attribute
Only Transporters with a valid CompanyId can access
/api/issue-tracking/{id}. Other users receive 401 Unauthorized.
Step 6: Testing
- Generate a JWT token with:
- Call the endpoint using Postman:
- Only Transporters with CompanyId succeed
- Other roles get 401 Unauthorized
Conclusion
This article shows how to:
- Secure method-level endpoints for Transporter users
- Extract UserId and CompanyId from JWT claims
- Use middleware to make claims easily accessible
- Separate Back-Office and Transporter access cleanly
This approach is scalable, clean, and ready for production microservices.
Best and Most Recommended ASP.NET Core 10.0 Hosting
Fortunately, there are a number of dependable and recommended web hosts available that can help you gain control of your website’s performance and improve your ASP.NET Core 10.0 web ranking. HostForLIFE.eu is highly recommended. In Europe, HostForLIFE.eu is the most popular option for first-time web hosts searching for an affordable plan. Their standard price begins at only €3.49 per month. Customers are permitted to choose quarterly and annual plans based on their preferences. HostForLIFE.eu guarantees “No Hidden Fees” and an industry-leading ’30 Days Cash Back’ policy. Customers who terminate their service within the first thirty days are eligible for a full refund.
By providing reseller hosting accounts, HostForLIFE.eu also gives its consumers the chance to generate income. You can purchase their reseller hosting account, host an unlimited number of websites on it, and even sell some of your hosting space to others. This is one of the most effective methods for making money online. They will take care of all your customers’ hosting needs, so you do not need to fret about hosting-related matters.