Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check what the current users role is

Tags:

c#

asp.net

How do I check in C# what the current users role is, and print it to the screen.

Thanks!

like image 737
anthonypliu Avatar asked Jan 31 '11 03:01

anthonypliu


People also ask

How do you find the current user role?

First off, we check that the user is actually logged in. If they're not logged in, they won't have a role assigned. If the user is logged in, we use wp_get_current_user to return the WP_User object. This provides us with a stack of information about the data and we can access their user role(s) via $user->roles .

How do I check my current role in mendix?

In system variable, you can use [%CurrentUser%] system variable. In case of module roles, you can create one entity for user roles, you can retrieve data from those user roles in the microflow . In retrieve activity, select retrieve by database, you can set xPath to identify the user role.

How do I get all user roles in WordPress?

There's also a function wp_dropdown_roles() which gives you the roles as option html elements. You can also set the default selected value by passing the role slug as a parameter. Show activity on this post. you would like to display a select list of available WordPress role names.

Where are user roles stored?

WordPress user roles are stored in the wp_options table.


1 Answers

The most general method is to get an IPrinciple and then call IsInRole() on it. How you get the Principle denpends on your runtime environment. This example works well for apps running under the user's account.

Example:

    static void PrintIsInAdministrators()
    {
        // There are many ways to get a principle... this is one.
        System.Security.Principal.IPrincipal principle = System.Threading.Thread.CurrentPrincipal;
        bool isInRole = principle.IsInRole("MyDomain\\MyRole");
        Console.WriteLine("I {0} an Admin", isInRole ? "am" : "am not");
    }
like image 178
ErnieL Avatar answered Sep 27 '22 16:09

ErnieL