From the course: ASP.NET MVC 5 Identity: Authentication and Authorization

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

View-based authorization using the view model

View-based authorization using the view model

From the course: ASP.NET MVC 5 Identity: Authentication and Authorization

Start my 1-month free trial

View-based authorization using the view model

- [Instructor] There are other ways of achieving the same goal without having to check the identity of a user from the view site. We can do that in the controller before calling the view, for example. You could use, for example, ViewBag and ViewData to paste a string from a controller to a view, or you can even use a ViewModel. So inside my Solution, I will create a new folder named ViewModels. Inside this folder, I will add a new CS file named MyViewModel. MyViewModel has a property named IsAdmin. We are going to use this property to check from our view site if the value is true or not. Then, go back to your HomeController. Inside the Controllers folder, HomeController. Scroll down to the Contact page. And in here, write the Authorize attribute, but define that this action can be accessed only from the users which belong to the Admin role. Then, since this action is going to be available only for the Admin roles, here, we can create a new instance to our ViewModel. For that, we write…

Contents