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
Jaimin ShethiyaPosted May 2, 2024, 12:51 PM
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
Naimish MakwanaPosted May 2, 2024, 4:53 AM
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:In the above code,
_contextis your database context, andUser.FindFirstValue(ClaimTypes.NameIdentifier)gives you the ID of the currently logged-in user.Thanks
Jayraj ChhayaPosted May 1, 2024, 9:52 AM
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.
By applying the Authorize attribute with specific roles to each method, you can control access based on the user's role effectively.