asp.net mvc -> Roles.IsUserInRole(username,role)

爷,独闯天下 提交于 2019-12-25 04:51:54

问题


Do we need to add any references or import any namespace for Roles.IsUserInRole(username,role) - > is it not giving the right result.

<% if(Model.Count < 1)
    {%>
        No User's Add Under You!
    <% } else {
        foreach (var item in Model) { %>
    <tr class="tblheader">
        <th> User Name </th>
        <th> Last Activity      </th>
        <th> Administrator     </th>
        <th> Base User    </th>
        <th> Print User    </th>
        <th> SDI User </th>
        <th> Edit User         </th>
    </tr>
    <% if(Model.Count < 1)
    {%>
        No User's Add Under You!
    <% } else {
        foreach (var item in Model) { %>

<td class="usertd">
            <%if(Roles.IsUserInRole(item.UserName,"Administrator")) { %>
             <asp:Image runat="server" ImageUrl="~/Content/images/buttons/btn_rnd_save.png" alt="Yes" />
            <% } else { %>
            <asp:Image runat="server" ImageUrl="~/Content/images/buttons/btn_Close.gif" alt="No" />
          <% } %>           
          </td>
            <td class="usertd">
            <%if(Roles.IsUserInRole(item.UserName,"BaseUser")) { %>
             <asp:Image runat="server" ImageUrl="~/Content/images/buttons/btn_rnd_save.png" alt="Yes" />
            <% } else { %>
            <asp:Image runat="server" ImageUrl="~/Content/images/buttons/btn_Close.gif" alt="No" />
          <% } %>           
          </td>

回答1:


Have you imported System.Web.Security

For more details on this you can check http://msdn.microsoft.com/en-us/library/bz1zy88e.aspx




回答2:


System.Web.ApplicationServices for .NET 4.0

System.Web.Security for .NET 3.5




回答3:


You need to enable the RoleManager in web.config, as it is disabled by default.

<roleManager enabled="true" defaultProvider="AspNetSqlRoleProvider">
  <providers>
    <clear/>
    <add connectionStringName="ApplicationServices" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
  </providers>
</roleManager>



回答4:


System.Web.ApplicationServices



回答5:


If you set a breakpoint at

IsUserInRole
, is it hit?

来源:https://stackoverflow.com/questions/3321787/asp-net-mvc-roles-isuserinroleusername-role

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!