?
Solved

ASP.Net MVC 4 C# -- User.IsInRole ?

Posted on 2013-06-19
4
Medium Priority
?
592 Views
Last Modified: 2015-12-08
Only "BUICK_LeadMan" or "BUICK_Supervisor"
can view the "About" link.

How can I add something to the below code like
|| (User.IsInRole("BUICK_Supervisor") ?

------------------------------------------------
    if (User.IsInRole("BUICK_LeadMan"))
    {
       <li>@Html.ActionLink("About", "About", "Home")</li>
    }
0
Comment
Question by:finance_teacher
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 8

Accepted Solution

by:
bhmahler earned 2000 total points
ID: 39259415
Are these roles or usernames?  If they are roles you will need to implement a custom RoleProvider class that inherits from RoleProvider.  You can then override the IsInRole method (as well as others) and verify if the user is in the roles.

Here is a decent walk through about it
http://kitsula.com/Article/Custom-Role-Provider-for-MVC
0
 

Author Comment

by:finance_teacher
ID: 39259828
I am using MVC4 simpleMembership with the below SYSTEM generated database tables.

Can I use http://kitsula.com/Article/Custom-Role-Provider-for-MVC without having to change my tables and existing AccountController.cs logic ?

If not, what could be used so I don't have to change alot ?
----------------------------------------------------------------------
webpages_UsersInRoles
UserProfile
webpages_Membership
webpages_Roles
0
 

Author Comment

by:finance_teacher
ID: 39262569
Below is what I currently use with my MVC4 simpleMembership with the above SYSTEM generated database tables.


Do you have an easier way ?
-----------------------------------------------------
@if (Request.IsAuthenticated)
{
    var userRoles = Roles.GetRolesForUser(User.Identity.Name);
   
    <li>@Html.ActionLink("HomePage", "Index", "Home")</li>
   
    var roles = new List<string> { "BUICK_Insepctor", "BUICK_LeadMan" };
    if (userRoles.Any(u => roles.Contains(u)))
    { <li>@Html.ActionLink("viewable_by_BUICK", "Index", "MAINT_WORK_REQ", new { area = "BUICK" }, null)</li> }
                                                                                                             
    var roles2 = new List<string> { "FORD_Insepctor", "FORD_LeadMan" };
    if (userRoles.Any(u => roles2.Contains(u)))
    { <li>@Html.ActionLink("viewable_by_FORD", "Index", "MAINT_WORK_REQ", new { area = " FORD" }, null)</li> }
                                                                                                                                                             
    <li>@Html.ActionLink("Test Link", "Index", "MAINT_WORK_REQ", new { area = "__Testing" }, null)</li>
}
0
 

Author Comment

by:finance_teacher
ID: 39292869
Please reply
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question