c# - Programmatically Assigning Roles to User in MVC 4 via Checkboxes -
i using mvc 4 razor engine , c# code behind logic. customised version of simplemembership database has been built meet requirements of website.
the website can create , edit roles , users intended, having problem assigning role user. search results provide code assign particular role user on account creation. our requirements not have this, instead need manually assign them using ui via website.
there plenty of tutorials asp.net (example below), finding difficult locate relevant tutorial mvc 4.
http://www.asp.net/web-forms/tutorials/security/roles/assigning-roles-to-users-cs
after reading above, asking following questions.
question one: there mvc tutorial assigning user roles via ui? if so, can provide link tutorial.
question two: if not have link tutorial, provide advice on following:
when administrator loads user management ui table load list of users, when click edit button particular user following take effect , pass data view called 'edit.cshtml'.
public actionresult edit(int id = 0) { var userprofile = _db.userprofiles.find(id); if (userprofile == null) { response.redirect("~/usermanagement/pagenotfound"); } return view(userprofile); } [httppost] [validateantiforgerytoken] public actionresult edit(userprofile userprofile) { if (modelstate.isvalid) { _db.entry(userprofile).state = entitystate.modified; _db.savechanges(); return redirecttoaction("index"); } return view(userprofile); }
within 'edit' view thinking of rendering partial view onto page checkboxes various roles within database. similar following:
@model ienumerable<projectname.dal.models.role> @foreach (var item in model) { @html.labelfor(m => item.rolename) <label class="bolder"> visible @html.checkboxfor(m => item.visible) <span class="lbl"></span> </label> } @using (html.beginform()) { @html.antiforgerytoken() @html.validationsummary(true) <fieldset> <!-- code here edit user details --> <div id="assignableroles"> @{ html.renderpartial("_assignableroles"); } </div> </fieldset> }
once relevant checkbox/es have been marked should update database , give user access particular area (as long authorisation attributes have been added accordingly).
i did find online, not sure add in controller or whether relevant predicament considering using 2 views need handle 2 different models.
var role = (simpleroleprovider)roles.provider; if (!role.roleexists(selectedrole)) role.createrole(selectedrole); websecurity.createuserandaccount(model.username, model.password); role.adduserstoroles(new[] { model.username }, new[] { selectedrole });
apologies long post, first time have used membership within mvc , still new tool.
edit:
i have ensured added web.config file:
<rolemanager enabled="true" defaultprovider="simpleroleprovider"> <providers> <clear /> <add name="simpleroleprovider" type="webmatrix.webdata.simpleroleprovider, webmatrix.webdata" connectionstringname="defaultconnection" applicationname="projectname" /> </providers> </rolemanager>
simpliest way manipulate following methods:
roles.addusertorole(username, rolename)
- add role
roles.removeuserfromrole(username, rolename)
- remove role
for example, add role user may have following action result in controller:
[httppost] public actionresult(string username, string rolename) { roles.addusertorole(username, rolename) return view() }
string username, string role - should passed view post method.
Comments
Post a Comment