c# User.IsInRole Namespace

12,193

Solution 1

Try first : while executing the view, check the following in controller HttpContext.Current.User.IsInRole("Admin") - this line check your value.

It should return a bool value if you have current HttpContext loaded.

Solution #2: Look at the default mvc3 project:

Context.User.IsInRole("Admin")

instead of Page.User.IsInRole("Admin").

In addition: you may check this post about how to set usage of roles - User.IsInRole(" ") without using Membership.

Look for the following usage with ASP.NET MVC Membership classes :

Solution 2

add using System.Web.Mvc; That should do it

So based on your comment, I'm going to assume you are working in a class that is not a Controller, but is inside your MVC project. So you should be able to do what you are attempting like so

if(HttpContext.Current.User.IsInRole("Admin"))
    {
      //...         
    }
Share:
12,193

Related videos on Youtube

Nate Pet
Author by

Nate Pet

Updated on September 15, 2022

Comments

  • Nate Pet
    Nate Pet over 1 year

    Note, I am using c# MVC 3, I am trying to use this within a class, NOT a controller.

    I have the following at top of my program

        using System.Web.Security;
    

    I tried to do the following but get the message:

    The name 'User' does not exist in the current context.

    Here is my partial code:

         using System.Web.Security;
         ....
         ....
    
         if (User.IsInRole("Admin"))  
         {
    
         }
    

    I am thinking that is has to do something with the namespace but looking at the documentation, all I should need is System.Web.Security.

    • Mark Coleman
      Mark Coleman over 11 years
      User is part of the context or Page, where are you trying to access User?
  • Forty-Two
    Forty-Two over 11 years
    Right click references in solution explorer and choose "add reference". Click .NET tab and find the System.Web.Mvc component name
  • JDB
    JDB over 11 years
    If you have to add a reference to System.Web.Mvc, then I don't think that you are working with a standard MVC project. Is it possible this code is in a Model layer that is a code library?
  • Nate Pet
    Nate Pet over 11 years
    @Cyborgx37 - I am using MVC but this code is inside of a class. Please advise. Thank you.
  • Nate Pet
    Nate Pet over 11 years
    Thanks. I do not see Context.User.IsInRole("Admin"). Note that I am using MVC and this code is not a controller but a class file. Please advise. Thanks
  • Nate Pet
    Nate Pet over 11 years
    @Forty-Two - I am using MVC but this code is inside of a class. Please advise. Thank you
  • JDB
    JDB over 11 years
    You say you are using MVC, but System.Web.Mvc should have been referenced by default (otherwise an MVC project wouldn't compile). Did you add a class library to your solution (perhaps as a DataTier or Model or whatchamacallit)?
  • Yusubov
    Yusubov over 11 years
    You would need to use Membership in order to be able to do Role checks in class.
  • Nate Pet
    Nate Pet over 11 years
    Where can I find out more on Membership.
  • Yusubov
    Yusubov over 11 years
    i have provided references in my answer, check them