5
I’m developing my first application in Asp.NET MVC and now creating the restriction and authorization part of users. I would like to do this so that the menus of my application are only accessible to certain users.
For example:
@if ("administrador")
{
<li>@Html.ActionLink("Agenda", "Index", "Agenda")</li>
<li>@Html.ActionLink("Criar", "Create", "Agenda")</li>
}
That is, in my layout identify which type of user can see that menu. So my questions are:
- Like assigning which users are administrators?
- How to create a controller identifying which user is logged in ( I use windows Authentication )
- Check which group it is part of? I don’t even use
Cookie
, norSession
in my application, it’s all via Windows Authentication?
I get user authentication through a class
public static class UserDetails
{
public static string GetMatricula(string userName)
{
string matricula = userName.Substring(userName.IndexOf(@"\") + 1);
return matricula;
}
}
And on the controller I have
public ActionResult Index()
{
var matricula = UserDetails.GetMatricula(User.Identity.Name);
var usuario = db.Usuarios.FirstOrDefault(x => x.Matricula == matricula);
}
The registration field is the same as the user logs into Windows. This way once the user enters the application, automatically already opens with its information from name and registration.
I edited the question @Gypsy Morrison Mendez. So, in this "Administrator" how do I inform which are the administrators? To do via controller as it would be? I need to say that such registrations are administrators, such are common users, and other advanced users, so that when entering the application is only visible the corresponding menu for each
– kLucas
@kLucas I edited the answer.
– Leonel Sanches da Silva
I still need help. I tried with ASP.NET Membership but didn’t have much success along with my user authentication mode. The model and the view that you passed in this answer are clear to me, but the Isinrole and Roleprovider of the link that passed the other answer no. There is considering login and password. I don’t have it. How would it be using windows Authentication? It’s still unclear to me, sorry
– kLucas
What matters most to me is to hide some users' menu
– kLucas
@kLucas Implement only the
CustomRoleProvider
. No need to implement theCustomMembershipProvider
(which is the one with the password).– Leonel Sanches da Silva