0

We protect action with authorize attribute with specific role name this way

[Authorize(Roles="members, admin")]

suppose users and roles are mapped in db table. so when user login then how could i attach role with logged in user using identity.

here i am posting url and sample which show how people do the same in mvc4 with custom form authentication. just see the code and i hope surely understand what i am trying to do with asp.net mvc 5 using identity. https://www.codeproject.com/Articles/408306/Understanding-and-Implementing-ASP-NET-Custom-Form see this above url for custom form authentication with asp.net mvc 4

protected void FormsAuthentication_OnAuthenticate(Object sender, FormsAuthenticationEventArgs e)
{
    if (FormsAuthentication.CookiesSupported == true)
    {
        if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
        {
            try
            {
                //let us take out the username now                
                string username = FormsAuthentication.Decrypt(Request.Cookies[FormsAuthentication.FormsCookieName].Value).Name;

                //let us extract the roles from our own custom cookie
                string roles = DBHelper.GetUserRoles(username);

                //Let us set the Pricipal with our user specific details
                e.User = new System.Security.Principal.GenericPrincipal(
                  new System.Security.Principal.GenericIdentity(username, "Forms"), roles.Split(';'));
            }
            catch (Exception)
            {
                //somehting went wrong
            }
        }
    }
}

i am working with asp.net mvc 5 & identity system. please help and guide me. thanks

Mist
  • 684
  • 9
  • 30

1 Answers1

0

You have to get logged in user id first

var UserName= await User.Identity.GetUserId()

then you can assign any role to that logged in user like

var _context = new ApplicationDbContext();
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(_context));
UserManager.AddToRole("UserName", "UserRole");
Frankenstine Joe
  • 311
  • 5
  • 14
  • i guess AddToRole add role to db table. how this line add role to generic principal of logged in user? – Mist May 11 '18 at 12:30
  • see this line of code `e.User = new System.Security.Principal.GenericPrincipal( new System.Security.Principal.GenericIdentity(username, "Forms"), roles.Split(';'));` how to do this with identity? – Mist May 11 '18 at 12:31
  • 1
    this link will help you achieve what you are trying https://gooroo.io/GoorooTHINK/Article/17736/How-to-create-and-assign-roles-in-aspnet-mvc-5/32014#.WvWPwIiFPIU and this https://stackoverflow.com/questions/21217528/how-to-assign-a-role-to-a-user-in-mvc5?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Frankenstine Joe May 11 '18 at 12:40
  • i just like to know is it automatic in mvc5 when user login then user specific roles will be fetched and assign to GenericPrincipal()? – Mist May 11 '18 at 13:09
  • if lazyload is false then how mvc will load data from userrole and role table after login.....how to handle this situation? – Mist May 11 '18 at 13:09