Search code examples
c#asp.net-mvcasp.net-mvc-3asp.net-membership

c# User.IsInRole Namespace


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.


Solution

  • 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 :