Solved

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

Posted on 2013-06-19
4
348 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
  • 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now