Total Page Preview:   000000000091

What is Authorize in Asp.Net Core MVC in English and Hindi

Authorize in English

In ASP.NET Core MVC, authorization is the process of controlling access to different parts of your web application based on user roles, permissions, or other criteria. The Authorize attribute is commonly used to specify which users or roles are allowed to access a particular controller action or an entire controller. Here's an example of how to use the Authorize attribute in ASP.NET Core MVC:
1-Create an ASP.NET Core MVC Project:
First, create a new ASP.NET Core MVC project using your preferred development environment (Visual Studio, Visual Studio Code, etc.).
2-Define User Roles:
Define user roles in your application. You can do this by configuring roles in your application's startup configuration. For example:
3-Apply Authorization to Controller Actions:
Use the Authorize attribute to restrict access to specific controller actions or entire controllers. Here's an example:
In the above code, the Authorize attribute is applied to two controller actions. The first action, AdminDashboard, can only be accessed by users in the "Admin" role, while the second action, UserProfile, can be accessed by users in either the "User" or "Admin" roles.
4-Apply Authorization to the Entire Controller:
You can also apply the Authorize attribute to the entire controller to restrict access to all actions within the controller:
In this example, only users in the "Admin" role will be able to access any action within the AdminController.
5-Configuring Policies (Optional):
Besides using role-based authorization, you can also create custom policies using the AuthorizationPolicy and Policy attributes for more fine-grained control over access to your actions.
6-Authentication and Role Assignment:
Users need to be authenticated and assigned roles for the authorization to work correctly. You can use ASP.NET Core Identity or other authentication mechanisms to achieve this.
Remember to configure authentication and roles properly in your ASP.NET Core application for the Authorize attribute to function as expected. Additionally, you may customize and extend authorization logic based on your application's requirements.
Authorize in Hindi
ASP.NET Core MVC mein "Authorize" ek aise attribute hai jo aapke web application ke alag-alag hisson tak pahunchne ki anumati ka niyantran karne ka prakriya hai, jo istemal karke aap niyamit user roles, permissions, ya aur kisi criteria ke adhar par pravesh ko niyantrit kar sakte hain. "Authorize" attribute ko ASP.NET Core MVC mein kis tarah se istemal kiya jata hai, uska ek udaharan niche diya gaya hai:
1-ASP.NET Core MVC Project Banaen:
Sabse pehle, aapke pasandida vikas evam vikas mulyankan (Visual Studio, Visual Studio Code, ityadi) ka upayog karke ek naya ASP.NET Core MVC project banaen.
2-User Roles Ko Pratipadit Karein:
Aap apne application mein user roles ko pratipadit karen. Is kaam ko aap apne application ke startup configuration mein roles ko configure karke kar sakte hain. Niche ek udaharan diya gaya hai:
3-Controller Actions Par Authorization Lagayen:
"Authorize" attribute ka upayog kisi specific controller action ya poora controller par pahunchne ki rok lagan ke liye karne ke liye hota hai. Niche ek udaharan hai:
Upar di gayi code mein "Authorize" attribute do controller actions par lagaya gaya hai. Pahla action, AdminDashboard, sirf "Admin" role ke users tak pahunch sakte hain, jabki dusra action, UserProfile, "User" ya "Admin" role ke users tak pahunch sakte hain.
4-Poore Controller Par Authorization Lagayen:
Aap poora controller par bhi "Authorize" attribute lagakar usmein ke saare actions tak pahunchne ki rok laga sakte hain:
Is udaharan mein, AdminController ke saare actions tak sirf "Admin" role ke users hi pahunch sakte hain.
5-Policies Ko Configure Karna (Vikalpon ke liye):
Role-based authorization ke alawa, aap apne actions tak pahunchne ki adhik samvedansheel niyantrana ke liye "AuthorizationPolicy" aur "Policy" attribute ka upayog karke custom policies bana sakte hain.
6-Authentication aur Role Assignment:
Authorization ka sahi se kaam karne ke liye, users ko saksham karna aur roles ko prassign karna avashyak hota hai. Aap ASP.NET Core Identity ya anya authentication upayog karke is kaam ko kar sakte hain.
"Authorize" attribute ka sahi se upayog karne ke liye, apne ASP.NET Core application mein authentication aur roles ko sahi se configure karna na bhulein. Aur yaad rakhein ki aap apne application ke avashyakataon ke adhar par authorization logic ko customize aur extend kar sakte hain.



Thank You

About Author

Brijesh Kumar

Database Developer

I have more then 6 years Experience in Microsoft Technologies - SQL Server Database, ETL Azure Cloud - Azure SQL Database, CosmosDB, Azure Data Factory, PowerBI, Web Job, Azure Function, Azure Storage, Web Apps, Powershall and Database Migration On-Premise to Azure Cloud.
LinkedIn :





Facebook User: