5

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.

Yusubov
  • 5,815
  • 9
  • 32
  • 69
Nate Pet
  • 44,246
  • 124
  • 269
  • 414
  • 1
    `User` is part of the context or `Page`, where are you trying to access `User`? – Mark Coleman Jan 09 '13 at 21:03
  • Sorry, is this an MVC web project? If so add `using System.Web.Mvc;` – Forty-Two Jan 09 '13 at 21:03
  • are you using asp.net web project or asp.net mvc ? – Yusubov Jan 09 '13 at 21:04
  • Where is this code? Inside a Controller method? Inside a class? – levelnis Jan 09 '13 at 21:06
  • @Forty-Two, Thanks, I am using MVC. I tried to do using System.Web.Mvc but my pogram didn't recognize it. I believe I need a reference. Where do I find the reference at? – Nate Pet Jan 09 '13 at 21:07
  • Get yourself a copy of ReSharper - it'll prompt you to add missing using statements automatically (amongst a great many other things) – levelnis Jan 09 '13 at 21:07
  • See [How to: Add or Remove References By Using the Add Reference Dialog Box](http://msdn.microsoft.com/en-us/library/wkze6zky.aspx). But if a reference does not already exist, then I doubt this will fix your problem. – JDB Jan 09 '13 at 21:09
  • @levelnis - I am using MVC, this is in a class – Nate Pet Jan 09 '13 at 21:20

3 Answers3

11

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 :

Community
  • 1
  • 1
Yusubov
  • 5,815
  • 9
  • 32
  • 69
4

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"))
    {
      //...         
    }
Forty-Two
  • 7,535
  • 2
  • 37
  • 54
  • Right click `references` in solution explorer and choose "add reference". Click .NET tab and find the System.Web.Mvc component name – Forty-Two Jan 09 '13 at 21:10
  • 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? – JDB Jan 09 '13 at 21:12
  • @Cyborgx37 - I am using MVC but this code is inside of a class. Please advise. Thank you. – Nate Pet Jan 09 '13 at 21:25
  • @Forty-Two - I am using MVC but this code is inside of a class. Please advise. Thank you – Nate Pet Jan 09 '13 at 21:38
  • 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)? – JDB Jan 09 '13 at 21:39
0

Use System.Web.HttpContext.Current.User.IsInRole("yourRole")

Hope it will help :)

Shujat Munawar
  • 1,657
  • 19
  • 23