Asp.Net Identity IsInRole not working or Returning Empty

Hi Kids,

If this piece of code in Asp.Net MVC 5 on .Net 4.6 is Not working

User.IsInRole("Admin");

Just code it like this.

       ApplicationUser u = await UserManager.FindByNameAsync(username);
       var roles = u.Roles;
       List<string> s = new List<string>();
       foreach (var i in roles)
       {
           var r = db.Roles.SingleOrDefault(w => w.Id == i.RoleId);
           s.Add(r.Name);                        
       }

       if (s.Contains("Admin"))
       {
          return RedirectToAction("Index","Jumpers");
       }

It will save you time.

Alright Kids! You’re Welcome!

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *