html - How to provide Role based Login and Menu in MVC3? -


My job is to provide a role-based menu to users ..... This is the first time that I work practically MVC3 is expecting your help .... I will fill the roles from the database ...

So I have such a separate menu [Home], [about] , [Projects], [setup] [resource] and what I want to do in this way, when a user logs in to the [administrator] role So I have to provide him with [home], [about], [setup] menu, how can I do this, help me in doing this briefly ... It is only when How did I get the name of role from role id in tables .... I created a login, where I am now using the role on the basis, how can I now provide the role based login < P> This is my login model class

  Arwajnik class LoginModel {[required] [Display (name = "User Name")] public string Username {get; Set; } [Required] [datatype (datatype password)] [display (name = "password")] public string password {get; Set; } [Display (name = "Remember me?")] Public Bull Memem {Received; Set; } Public bool isvalid (string_useeman, string_pwd) {SqlConnection cn = New SqlConnection ("Data Source = LMIT-0039; Initial Catalog = Bug Tracker; Integrated Security = True"); Cn.Open (); SqlCommand cmd = New SqlCommand ("GetRoleName", cn); Cmd.CommandType = CommandType.StoredProcedure; Cmd.Parameters.Add ("@UsernameName", SqlDbType.VarChar) .Value = _username; Cmd.Parameters.Add ("@password", SqlDbType.VarChar) .Value = _pwd; Object typeuser = CMD. Exclaimer (); Cn.Close (); If (typeduser! = Null) {if (typeUser.ToString () == "Admin") returns true; Else if (typeUser.ToString () == "developer") return true; Otherwise if (typeUser.ToString () == "QA") returns true; Second false return; } Back true; }   

Anyone can help me do this ....

If you use built-in form authentication, you can see user roles in this way:

  string userRole = roles. Petrolforce user (User.Identity.Name). FirstOrDefault ();   

I used FirstOrDefault because a user can have more than one role. To return the view related to here you can switch to switch statement, in addition, if you want to hide objects (such as menu items in global layout), then you can:

  @if (roles.GetrolLoForce User (user.indentityname) .FirstOrDefault () = "TheRoleYouWant") {...}   

its al Or you can add a authorized attribute to a function in a controller (or overall controller) that only allow users who use this role to access it like this: < Pre> [authorized (role = "admin, roll 2, roll 3")) Public Performance Index () {Return View (); }

Note that you can add more than one role, they should be isolated from above commas.

There are several options, such as take a look at ISUser Inroll ("Admin") .

Comments