Part 70 Authorize and AllowAnonymous action filters in mvc

preview_player
Показать описание
Link for code samples used in the demo

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

In this video, we will discuss Authorize and AllowAnonymous action filters in mvc.

In ASP.NET MVC, by default, all the controller action methods are accessible to both anonymous and authenticated users. If you want action methods, to be available only for authenticated and authorised users, then use Authorize attribute. Let us understand "Authorize" and "AllowAnonymous" action filters with an example.

2. Right click on the "Controllers" folder and add HomeController. Copy and paste the following code.
public class HomeController : Controller
{
public ActionResult NonSecureMethod()
{
return View();
}

public ActionResult SecureMethod()
{
return View();
}
}

3. Right click on NonSecureMethod() and add a view with name = NonSecureMethod. Similarly add a view with name = SecureMethod.

4. Associate MVCDemo project with IIS.
a) Right click on the project name in "solution explorer" and select "Properties"
b) Click on "Web" tab
d) Click "Create Virtual Directory" button

5. Open IIS. Expand "Sites" and then "Default Web Site" and select "MVCDemo". Double click on "Authentication" icon. Enable "Anonymous Authentication" and "Windows Authentication", if they are not already enabled.

6. At this point, you will be able to access, both "SecureMethod" and "NonSecureMethod", by visiting the following URLs.

7. If you want "SecureMethod" to be available only for authenticated users, then decorate it with "Authorize" attribute.
[Authorize]
public ActionResult SecureMethod()
{
return View();
}

9. Now remove the [Authorize] attribute from SecureMethod(), and apply it on the HomeController.
[Authorize]
public class HomeController : Controller
{
public ActionResult NonSecureMethod()
{
return View();
}

public ActionResult SecureMethod()
{
return View();
}
}

At this point, "Authorize" attribute is applicable for all action methods in the HomeController. So, only authenticated users will be able to access SecureMethod() and NonSecureMethod().

10. To allow anonymous access to NonSecureMethod(), apply [AllowAnonymous] attribute. AllowAnonymous attribute is used to skip authorization enforced by Authorize attribute.
[AllowAnonymous]
public ActionResult NonSecureMethod()
{
return View();
}
Рекомендации по теме
Комментарии
Автор

What i found in all videos that you were with - Clean and Clear Concept...Explanation!!

ManojKumar-rqqy
Автор

Hi Praven, sure, in our upcoming videos we will discuss consuming WCF services in an MVC application.

Csharp-video-tutorialsBlogspot
Автор

Hi Venkat, Thanks for your reply and for recorded the "Area" within a short period of 2 days.
you are really doing great job.

arunntbe
Автор

Good and understandable language. Good voice. Simple humble

bibinrajan
Автор

Hi Venkat, your way of explanation is very simple and straight forward which makes me understand the concept directly just in one time viewing ... .great dude, you rock. keep going, :)

ibknl
Автор

Hi Venkat. Really appreciate your for hard work to record such a great series.
Can you please record videos on Area? I wanted to know how routing works for Area.
Thanks a lot !!!

arunntbe
Автор

this is very good but i have question. If i applied authorize attribute at action method level and mentioned it in web config to redirect login page then how can i access that method without doing any anything in iis.

bkv
Автор

Hi venkat,
I could see exception filters topic is not there in the mvc5 tutorial videos. Could you please help me on that topic

kalyaniketepalli
Автор

We would really like you to make a video on ASP.NET 3 Tier Architechture.

jimrock
Автор

Hi Venkat thanks a lot for your videos, When i open the iis website i do not see a windows authentication option in the authentication list, what should i do ?

coding-gemini
Автор

Hi sir,
I have little bit confusion about windows credential, is it Client machine 's credential or server credential.

marutishrivastava
Автор

I am using webapi. The controller inherits basecontroller and basecontroller inherits controller(mvc). I can still access all methods even after adding Authorize attribute. What Can be reason ? Is there anything needs to be added to web.config?

shreeprasadlohar
Автор

Thanks nice video. In MVC if we have a static content I.e our employee image, Can we protect them accessing from outside by giving the url to the image file. Can we use mvc security identity provider or any web confit setting to restrict that? Please let me know

Thank you very much

shehanfernando
Автор

Nice video, but none of this authorization / allowanonymous is working for me. My project always redirects to the Login page. The only way I can allow the default page Index is to delete all Authorize attributes. But, then I get errors with my api/me and google ad errors. please help.

RideWithTheWolf
Автор

Hi Venkat, Can you please make a video to create a com object which can register in another pc in 64 bit(regsvr) for using in a Delphi 64 bit platform Application.

sankarbhutia
Автор

That's great. But what is difference between allow anonymous attribute actions and actions without any filters

lalithab
Автор

Please help me with this..
How many allow/denys can be used in each authorization?

romilshah
Автор

I can't get IIS 10 to work on my Windows 10 Pro machine. I've tried everything. It used to work but now only gives me this:

This site can’t be reached

localhost refused to connect.
Try:
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_REFUSED

I can run the VS development server but the [authorize] attribute doesn't do anything. I can access SecureMethod and don't get the logon dialog. I may have to rebuild this project on Windows 7. I hate IIS and I hate having to do these tutorials on Win 7 because some don't work in Win 10 and IIS 10!

BackpackandGear
Автор

[AllowAnonymous ] tag is not working in mvc 3, [Authorize] is working though any suggestions?

davidb
Автор

Hi bro, when i have a database (account table) and i use Authorize with role allow go to page follow user or admin. How i do it

nguyenhoan