Solved

asp.net mvc5 OWIN

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
asp.net get gridview datakey from row command 2 45
asp.net mvc5 6 33
How do I "share" on social sites? 2 38
ASP.NET VB code for check and insert in a table 4 44
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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