i ask same question 2-3 days ago someone give answer but that answer not usefull can somone help me now agai ffrom same question
i want when user come first login after that according when login person is admin so they access all application but when login person is seller so the access only DisplayProduct method,Index method or Add to cart method but in add to cart seller access only themself added product and when login person is user so they access only Display product Data and added themself item on addtocart method
Loading

Jayraj ChhayaRank #304 · 8.2K reputationPosted May 1, 2024, 5:52 AM EDT
To achieve role-based access control in a Dot Net application, you can utilize ASP.NET Identity for user authentication and authorization. Define roles for admin, seller, and user, and assign these roles to users upon login. Then, use Authorize attribute with roles to restrict access to specific methods based on the user's role.
[Authorize(Roles = "Admin")] public IActionResult AllApplication() { // Code for admin access } [Authorize(Roles = "Seller")] public IActionResult DisplayProduct() { // Code for seller access } [Authorize(Roles = "Seller")] public IActionResult Index() { // Code for seller access } [Authorize(Roles = "Seller")] public IActionResult AddToCart() { // Code for seller access to their own products } [Authorize(Roles = "User")] public IActionResult DisplayProductData() { // Code for user access } [Authorize(Roles = "User")] public IActionResult AddToCart() { // Code for user access to add their own items to cart }By applying the Authorize attribute with specific roles to each method, you can control access based on the user's role effectively.
Naimish MakwanaRank #324 · 7.7K reputationPosted May 2, 2024, 12:53 AM EDT
In .NET, you can use the built-in role-based authorization to control access to different parts of your application based on the user’s role. Here’s a high-level overview of how you can implement this:
Define Roles: Define roles such as “Admin”, “Seller”, and “User” in your system.
Assign Roles: When a user logs in, assign them a role based on their credentials.
Protect Methods: Use the
[Authorize(Roles = "RoleName")]attribute to protect your methods. For example:[Authorize(Roles = "Admin")] public IActionResult SomeAdminMethod() { // ... } [Authorize(Roles = "Seller")] public IActionResult DisplayProduct() { // ... } [Authorize(Roles = "Seller, User")] public IActionResult Index() { // ... } [Authorize(Roles = "Seller, User")] public IActionResult AddToCart(int productId) { // Check if the user is a seller and if they are the owner of the product if (User.IsInRole("Seller")) { var product = _context.Products.Find(productId); if (product.SellerId != User.FindFirstValue(ClaimTypes.NameIdentifier)) { return Unauthorized(); } } // Add the product to the cart // ... return Ok(); }In the above code,
_contextis your database context, andUser.FindFirstValue(ClaimTypes.NameIdentifier)gives you the ID of the currently logged-in user.Thanks
Jaimin ShethiyaRank #177 · 13.7K reputationPosted May 2, 2024, 8:51 AM EDT
Hello Soheb,
Please refere the below links, in that help you how to implement the role based authorization in .net core application.
https://learn.microsoft.com/en-us/aspnet/core/security/authorization/roles?view=aspnetcore-8.0
https://dotnetcorecentral.com/blog/asp-net-core-authorization/
https://www.yogihosting.com/aspnet-core-identity-roles/
https://jasonwatmore.com/post/2022/02/18/net-6-role-based-authorization-tutorial-with-example-api
https://medium.com/@siva.veeravarapu/role-based-authorization-in-net-core-a-beginners-guide-with-code-snippets-b952e5b952f7
Thanks