Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2013-06-19
4
Medium Priority
?
660 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

604 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