Solved

asp.net mvc5 OWIN

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

947 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

20 Experts available now in Live!

Get 1:1 Help Now