简体   繁体   English

更改ASP.NET身份注册用户以允许多个角色

[英]Change ASP.NET Identity register user to allow multiple roles

Right now my code uses the default ASP.NET code to register a user. 现在,我的代码使用默认的ASP.NET代码注册用户。 It displays the list of roles on the view using the following code 它使用以下代码在视图上显示角色列表

    <div class="form-group">
         @Html.LabelFor(model => model.Role, "Role", new {@class = "col-md-2 control-label"})
         <div class="col-md-10">
             @Html.DropDownListFor(model => model.Role, Model.Roles, "--Select Role --")
         </div>
     </div>

I want to allow the person that is creating the account to select multiple roles. 我想允许创建帐户的人选择多个角色。 Any suggestion on how to change this to a different solution? 关于如何将其更改为其他解决方案的任何建议? I was thinking check boxes would be the most ideal. 我当时认为复选框将是最理想的。

You could use check boxes; 您可以使用复选框; but then you need to draw a checkbox and label for every role in your application. 但随后您需要为应用程序中的每个角色绘制一个复选框并标记。 If you ever add roles depending on how you drew them there may be an update required there. 如果您根据角色的添加方式添加角色,则可能需要在那里进行更新。

I would choose to use a mutliselect widget/input/dropdown; 我会选择使用mutliselect小部件/输入/下拉菜单; there are many available from places like kendo core and jqueryUI. kendo core和jqueryUI等地方有很多可用的工具。 You could bind the population of this widget to an Ajax method, Api call, viewModel binding, or viewbag binding. 您可以将此小部件的填充绑定到Ajax方法,Api调用,viewModel绑定或viewbag绑定。 Then update the save method to go through the collection of roles to add the user to that you received from the multiselect. 然后更新保存方法以遍历角色集合,以将用户添加到您从多选接收的用户中。 You can bind the results to an IEnumerable with the kendo one automatically. 您可以使用剑道自动将结果绑定到IEnumerable。

Additionally you need to check the roles they are currently in and remove any that the user no longer has checked, and the identity/role methods will have issues if you are already in the role its trying to add you to. 另外,您需要检查他们当前所处的角色,并删除用户不再检查的任何角色,如果您已经在尝试将您添加到该角色中,则身份/角色方法将出现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM