Quantcast
Channel: Security
Viewing all articles
Browse latest Browse all 4737

adding users to a role

$
0
0

Dear reader,

As many people on the internet I have some troubles with adding users to roles. What is a useful solution to this problem?

This is my controller:

       public ActionResult Toevoegen(string RoleID)
        {
            ViewBag.RoleID = RoleID;
            ViewBag.RoleNaam = db.AspNetRoles.Find(RoleID).Name.ToString();
            return PartialView("_Toevoegen");
        }
        public ApplicationUserManager UserManager
        {
            get { return UserManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); }
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Toevoegen(string UserName, string RoleNaam, string RoleID)
        {
            if (ModelState.IsValid)
            {
                var user = db.AspNetUsers.Where(a => a.UserName == UserName).First();
                string userID = user.Id;

                var account = new AccountController();

                account.UserManager.AddToRoleAsync(userID, RoleNaam);

                //ViewBag.ResultMessage = "Role created successfully !";

                // prepopulat roles for the view dropdown
                string url = Url.Action("RollenGebruikers", "Rollen", new { id = RoleID });
                return Json(new { success = true, url = url });
            }

            return PartialView("_Toevoegen");
        }

My view:

<div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button><h4 class="modal-title" id="myModalLabel">Voeg nieuwe gebruiker toe aan rol</h4></div>


@using (Html.BeginForm("Toevoegen", "Rollen"))
{<div class="modal-body">

        @Html.AntiForgeryToken()
        <div class="form-horizontal"><div class="form-group"><b>Gebruikersnaam</b><div class="col-md-10">
                    @Html.TextBox("UserName")
                    @Html.Hidden("RoleID", (string)ViewBag.RoleID, null)
                    @Html.Hidden("RoleNaam", (string)ViewBag.RoleNaam, null)</div></div></div></div><div class="modal-footer"><button class="btn" type="button" data-dismiss="modal">Cancel</button><input class="btn btn-primary" type="submit" value="Add" /></div>
}

And I encounter the following error:

System.NullReferenceException: De objectverwijzing is niet op een exemplaar van een object ingesteld.

Regel 49:             get
Regel 50:             {Regel 51:                 return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();Regel 52:             }
Regel 53:             private set

Viewing all articles
Browse latest Browse all 4737

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>