Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 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

23 Experts available now in Live!

Get 1:1 Help Now