Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Windows authentication and authorization in asp.net - Part 88

Suggested Videos
Part 85 - Anonymous authentication
Part 86 - Anonymous authentication and asp.net impersonation
Part 87 - Windows authentication

In Part 87, we have discussed the basics of windows authentication. In this session, we will continue to discuss about windows authentication. Please watch Part 87, before proceeding.

? and * have special meaning when used in the authorization element in web.config
? (Question Mark) - Indicates anonymous users
* (Star) - Indicates all users



Allowing or denying access to specific users:
When you run the application, with the following authorization list in web.config, only users "Venkat" and "Pragim" are allowed to access the application. If you are logged, into the computer, as any other user, the application prompts the user to provide user name and password. All the other users are denied access to the application.
<authorization>
  <allow users="Prasad-PC\Venkat, Prasad-PC\Pragim"/>
  <deny users="*"/>
</authorization>



Using windows roles to control access:
Windows operating system has several roles, like Administrators, Guests, Users etc. It is also possible to control access to resources using these roles in the web.config file. The following authorization list, only allows users belonging to Administrators role. All the other users are denied access.
<authorization>
  <allow roles="Administrators"/>
  <deny users="*"/>
</authorization>

How to programmatically check if the user belongs to a specific role?
if (User.IsInRole("Administrators"))
{
    // Do Admin Stuff
}
else
{
    // Do Non-Admin stuff
}

1 comment:

It would be great if you can help share these free resources