Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 258
Nate Pet Avatar asked Jan 09 '13 21:01

Nate Pet


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


2 Answers

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 :

  • Membership and Authorization in ASP.Net MVC 3 Razor
  • SimpleMembership, Membership Providers, Universal Providers and the new ASP.NET 4.5 Web Forms and ASP.NET MVC 4 templates
  • Asp.net membership - how to determine programmatically is user is in role
  • ASP.NET MVC Membership Roles
like image 114
Yusubov Avatar answered Sep 27 '22 22:09

Yusubov


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"))
    {
      //...         
    }
like image 40
Forty-Two Avatar answered Sep 28 '22 00:09

Forty-Two