Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

asp.net mvc5 OWIN

Posted on 2016-08-16
2
Medium Priority
?
78 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 19

Accepted Solution

by:
Manoj Patil earned 2000 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

722 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