I m working on ASP.NET MVC 4 application.I have a dashboard and my users groups will be based on Windows Domain So I am using WIndows Authentication for authenticating users. I created sample applications which uses custom authentication by overrides functions AuthorizeAttribute, ActionFilterAttribute . Is this a good approach ?
- Which attribute is best used for authentication ?
I have a dashboard. So I need to show or hide the controls based on roles. Suppose if there is 3 grids(table), If Admin is logs in, he can able see 3 grids(tables). But if Support user is log in he can see 2 grids (table) only.
My plan is to create partial views for each grid and so there will be an Action and Controller for each partial view. There will be a database and in that I will specify the actions which each group can perform. So that I can filter the requests.
2 How can I hide or show the partial views based on roles ?.
I tried some SO links, but all they are talking about 2,3 roles and it was hard coded. In my case roles may vary and we uses db to set up access for roles.
Thanks in advance.
Typically you would want to keep your views as clean as possible with little to no logic. I would suggest moving your role checking logic into a controller action and rendering a partial view based on the users role.
You can use ChildActions and the Html.Action extension method to get this wired up.
From MSDN:
In your project, create a new Controller called Dashboard and added a single Action called BuildTable.
Include the following line in the view where you want the dashboard table to appear.
You can use Following code for role based checking
I have done something similar. The way I did it (may not be the best)
is to send a boolean back to the view
in the controller use:
then in the view:
you will need to do the logic to set the boolean the way you want them but this should be a start.
you could also create a static method that returns the role and then get that value directly from the view. this may be bad form though.
then create a class called AppHelper and a method called GetRole that returns the role of the user.