Maruf Ahammed Juwel
Maruf Ahammed Juwel

Reputation: 159

How to get current logged in user's roleID in MVC5

As every one knows we can get login UserId and UserName by

string curentUser = User.Identity.GetUserId();
string currentUserName = User.Identity.GetUserName();

But how to get current user's roleId.

Upvotes: 1

Views: 2577

Answers (3)

Sanket Shahane
Sanket Shahane

Reputation: 1

Microsoft.AspNetCore.Mvc namespace provides public property User of ClaimsPrincipal type which hold the information of current logged in user , to get current role id ClaimsPrincipal class have static method FindFirstValue you can use that e.g. string User.FindFirstValue(ClaimTypes.Role);

Upvotes: 0

Sam FarajpourGhamari
Sam FarajpourGhamari

Reputation: 14741

In Identity you could use UserManager to access each users roles as well. Since roles already have been implemented therefor you don't need any extra configuration. So consider this example:

public ActionResult MyAction()
{
    userManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();

    // return a list of current user's roleIDs
    var roles = userManager.FindById(User.Identity.GetUserId()).Roles.Select(r => r.RoleId);
}   

Upvotes: 1

jhmt
jhmt

Reputation: 1421

You can get the roles the member has like this:

using System.Web.Security;    
string[] roles = Roles.GetRolesForUser(User.Identity.Name);

A user can have multiple roles so the type of return value is string[].

UPDATED
In ASP.NET Identity 2.0, you can enable RoleManager as the following example:

Models/IdentityModels.cs

public class ApplicationRole : IdentityRole
{
}

App_Start/IdentityConfig.cs

public class ApplicationRoleManager : RoleManager<ApplicationRole, string>
{
    public ApplicationRoleManager(IRoleStore<ApplicationRole, string> store)
        : base(store)
    {
    }
    public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
    {
        var dbContext = context.Get<ApplicationDbContext>();
        var roleStore = new RoleStore<ApplicationRole>(dbContext);
        var manager = new ApplicationRoleManager(roleStore);

        // Add some roles (e.g. "Administrator") if needed
        if (!manager.Roles.Any(r => r.Name == "Administrator"))
        {
            manager.Create(new ApplicationRole
            {
                Name = "Administrator"
            });
        }
        return manager;
    }
}

App_Start/Startup.Auth.cs

public partial class Startup
{
    public void ConfigureAuth(IAppBuilder app)
    {
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager≥(ApplicationUserManager.Create);

        // Add this line
        app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
    }
}

Controllers/AccountController.cs

public class AccountController : Controller
{
    private ApplicationUserManager _userManager; 
    private ApplicationRoleManager _roleManager;

    public AccountController()
    {
    }

    public AccountController(ApplicationUserManager userManager, ApplicationRoleManager roleManager, ApplicationSignInManager signInManager)
    {
        UserManager = userManager;
        RoleManager = roleManager;
        SignInManager = signInManager;
    }

    public ApplicationRoleManager RoleManager
    {
        get
        {
            return _roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>();
        }
        private set
        {
            _roleManager = value;
        }
    }
}

Then, you can get roles from UserId in Account controller.

var roles = UserManager.GetRoles(User.Identity.GetUserId());

Upvotes: 1

Related Questions