Solved

asp.net mvc5 OWIN

Posted on 2016-08-16
2
38 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now