TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Jæÿ Pændyã
NA
11
738
Trying to get Roles to populate a checkboxlist
Feb 22 2017 4:31 PM
hi i am Trying to get Roles to populate a checkboxlist based on tutorial (https://weblogs.asp.net/scottgu/Recipe_3A00_-Implementing-Role_2D00_Based-Security-with-ASPNET-20-using-Windows-Authentication-and-SQL-Server)
but using Asp.net identity and c#
My code:
using CentretapCrmSystem.Models;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CentretapCrmSystem.Pages
{
public partial class Removeroles : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!User.IsInRole("Admin"))
{
Page.Visible = false;
Response.Redirect("accessdenied.aspx");
}
}
catch (HttpException)
{
}
}
public void PopulateRoleList(string userName)
{
RoleList.Items.Clear();
var Usermanager = Context.GetOwinContext().GetUserManager();
var RoleManager = new RoleManager
(new RoleStore
(new ApplicationDbContext()));
string[] roleNames = null;
string roleName = null;
var roles = RoleManager.Roles.ToList();
var d= roles.ToArray();
roleNames = d;
foreach (string roleName_loopVariable in roleNames)
{
roleName = roleName_loopVariable;
ListItem roleListItem = new ListItem();
roleListItem.Text = roleName;
roleListItem.Selected = Usermanager.IsInRole(userName, roleName);
RoleList.Items.Add(roleListItem);
}
}
public void UpdateRolesFromList()
{
foreach (ListItem roleListItem in RoleList.Items)
{
var Usermanager = Context.GetOwinContext().GetUserManager();
var RoleManager = new RoleManager
(new RoleStore
(new ApplicationDbContext()));
string roleName = roleListItem.Value;
string userName = DropDownList1.SelectedValue;
bool enableRole = roleListItem.Selected;
if ((enableRole == true) & (Usermanager.IsInRole(userName, roleName) == false))
{
Usermanager.AddToRole(userName, roleName);
}
else if ((enableRole == false) & (Usermanager.IsInRole(userName, roleName) == true))
{
Usermanager.RemoveFromRole(userName, roleName);
}
}
}
protected void LookupBtn_Click1(object sender, EventArgs e)
{
PopulateRoleList(DropDownList1.SelectedValue);
UpdateBtn.Visible = true;
}
public void UpdateBtn_Click(object sender, System.EventArgs e)
{
UpdateRolesFromList();
PopulateRoleList(DropDownList1.SelectedValue);
}
}
}
the problem with using asp.identy is i can no use roles.getallroles() mathod which returns roles in string[]. can you help me what should i use for
roleNames to make my code work
and how to get all roles in string arry[] in identity
Reply
Answers (
0
)
how to use nhibernate in asp.net web api
Onclick Button hide row forever in GridView ASP.NET