Solved

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

Posted on 2013-06-19
4
501 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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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