Solved

asp.net mvc5 OWIN

Posted on 2016-08-16
2
52 Views
Last Modified: 2016-08-18
HI guys,

I have implemented user authentication and used owin by .Net framework.

I can't get the concept of how to pass roles to view. The framework gives - var rol = Usermanager.GetRoles(id); to get all roles by user id, but when I get the roles I don't really know how to get these roles and compare them to my model.

Please, help.

Thanks,
0
Comment
Question by:Moti Mashiah
2 Comments
 
LVL 19

Accepted Solution

by:
Manoj Patil earned 500 total points
ID: 41759215
Hey try this

public ActionResult CreateUser()
{
   var model = new CreateUserViewModel();
   ApplicationDbContext appDbContext = new ApplicationDbContext();

   model.Roles = appDbContext.Roles.Select(r => new lb_pgin_bprp.Models.ApplicationRole { Id = r.ID, Name = r.Name }).ToList();
   return View();
}

Also check below
http://stackoverflow.com/questions/30434735/how-to-pass-user-roles-to-view-as-viewmodel-in-asp-net-mvc
http://techbrij.com/role-based-menu-asp-net-mvc
0
 
LVL 1

Author Closing Comment

by:Moti Mashiah
ID: 41761158
Was helpful.
Look at my full solution:
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
            var Rolemanager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));

            var usr = UserManager.FindById(model.Id);

            usr.FirstName = model.FirstName;
            usr.LastName = model.LastName;
            usr.Email = model.Email;
            UserManager.Update(usr);


            if (admin != null)
            {
                UserManager.AddToRoles(model.Id, "Admin");
            }
            else
            {
                UserManager.RemoveFromRole(model.Id, "Admin");
            }
            if (user != null)
            {
                UserManager.AddToRoles(model.Id, "Users");
            }
            else
            {
                UserManager.RemoveFromRole(model.Id, "Users");
            }
            if(supervisors != null)
            {
                UserManager.AddToRoles(model.Id, "Supervisors");
            }
            else
            {
                UserManager.RemoveFromRole(model.Id, "Supervisors");
            }
0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question