如何在ASP.NET MVC4中创建用于创建用户角色的视图 [英] How to create a View for creating User Roles in ASP.NET MVC4

查看:72
本文介绍了如何在ASP.NET MVC4中创建用于创建用户角色的视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我对使用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.

推荐答案

创建新角色ASP.NET MVC,也许你可以尝试这段代码:

To create a new role in ASP.NET MVC,maybe you can try this code :
@{
    ViewBag.Title = "RoleCreate";
    Layout = "~/Views/Shared/_LayoutAdmin.cshtml";
}
<div class="spacerBody">
    <p>&nbsp;</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 MVC 5中实现基于角色的身份验证 [ ^ ]



希望这可以帮助你...
Refer

Extending Identity Accounts and Implementing Role-Based Authentication in ASP.NET MVC 5[^]

Hope this helps you...


这篇关于如何在ASP.NET MVC4中创建用于创建用户角色的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆