问题描述
大家好,
我对使用ASP.NET MVC4开发互联网应用程序非常陌生。我想在我们的应用程序中使用SimpleMembership进行身份验证和授权。现在我想让Admin从前端定义角色。并在创建用户时将这些角色分配给用户。我们怎么做到这一点。换句话说,我们如何创建用户角色并使用ASP.NET MVC4简单成员资格将这些角色分配给用户。任何人都可以帮助我。
提前致谢。
Hi All,
I am very new to developing an internet application using ASP.NET MVC4. I would like to use SimpleMembership for authentication and authorization in our application. Now I want to allow the Admin to define the roles from front end. and also assign those roles to the users while creating a user. How can we do that. in other words, How can we create User role and assign those roles to user using ASP.NET MVC4 Simple membership . Can any one help me out.
Thanks in advance.
推荐答案
@{
ViewBag.Title = "RoleCreate";
Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
}
<div class="spacerBody">
<p> </p>
@Html.ActionLink("Roles", "RoleIndex") | @Html.ActionLink("Add Role to User", "RoleAddToUser")
<h2>Role Create</h2>
@using(Html.BeginForm()){
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div>
Role name</div>
<p>
@Html.TextBox("RoleName")
</p>
<input type="submit" value="Save" />
}
</div>
之后,您可以使用此代码创建新角色进入你的数据库。
复制这段代码:
After that,You can use this code to create a new Role into your database.
copy this code :
[Authorize(Roles = "Admin")]
public ActionResult RoleCreate()
{
return View();
}
[Authorize(Roles = "Admin")]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult RoleCreate(string RoleName)
{
Roles.CreateRole(Request.Form["RoleName"]);
// ViewBag.ResultMessage = "Role created successfully !";
return RedirectToAction("RoleIndex", "Account");
}
这篇关于如何在ASP.NET MVC4中创建用于创建用户角色的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!