本文整理汇总了C#中RoleManager.FindByName方法的典型用法代码示例。如果您正苦于以下问题:C# RoleManager.FindByName方法的具体用法?C# RoleManager.FindByName怎么用?C# RoleManager.FindByName使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类RoleManager
的用法示例。
在下文中一共展示了RoleManager.FindByName方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DeleteRole
public ActionResult DeleteRole(string roleName)
{
//ApplicationDbContext is the a classe is used by the ASP.NET Identity to manage user records
var context = new ApplicationDbContext();
var roleStore = new RoleStore<IdentityRole>(context);
var roleManager = new RoleManager<IdentityRole>(roleStore);
var deletedRole = roleManager.FindByName(roleName);
roleManager.Delete(deletedRole);
return RedirectToAction("RolesIndex", "Account");
}
示例2: Details
// GET: AspNetUsers/Details/5
public async Task<ActionResult> Details(string id)
{
AspNetUserDetailsViewModel mdl = new AspNetUserDetailsViewModel();
if(id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
mdl.User = await db.AspNetUsers.FindAsync(id);
if(mdl.User == null)
{
return HttpNotFound();
}
mdl.Roles = await db.AspNetRoles.ToListAsync();
var ctx = new ApplicationDbContext();
var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(ctx));
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(ctx));
if(userManager.IsInRole(mdl.User.Id, "NetUser"))
{
mdl.RoleId = RoleManager.FindByName("NetUser")?.Id;
}
if(userManager.IsInRole(mdl.User.Id, "Admin"))
{
mdl.RoleId = RoleManager.FindByName("Admin")?.Id;
}
return View(mdl);
}
示例3: InitDefaultUsersAndRoles
private void InitDefaultUsersAndRoles(UserManager<QuestionsAnswersUser> userManager,
RoleManager<IdentityRole> roleManager)
{
if (roleManager.FindByName("admin") == null)
{
roleManager.Create(new IdentityRole("admin"));
}
if (roleManager.FindByName("user") == null)
{
roleManager.Create(new IdentityRole("user"));
}
if (userManager.FindByName("admin") == null)
{
var user = new QuestionsAnswersUser {UserName = "admin"};
var result = userManager.Create(user, "adminadmin");
if (result.Succeeded)
{
userManager.AddToRole(user.Id, "admin");
}
}
userManager.Users.Where(u => !u.Roles.Any()).ToList().ForEach(u => userManager.AddToRole(u.Id, "user"));
}
示例4: DefaultUser
public static void DefaultUser(ApplicationDbContext db)
{
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(db));
var name = AppConfig.DefaultUser;
var pwd = AppConfig.DefaultUserPassword;
const string adminRole = "Admin";
const string dashboardRole = "Dashboard";
const string investigateRole = "Investigate";
//Create Role Admin if it does not exist
var ar = roleManager.FindByName(adminRole);
if (ar == null)
{
ar = new IdentityRole(adminRole);
var roleresult = roleManager.Create(ar);
}
var dr = roleManager.FindByName(dashboardRole);
if (dr == null)
{
dr = new IdentityRole(dashboardRole);
var roleresult = roleManager.Create(dr);
}
var ir = roleManager.FindByName(investigateRole);
if (ir == null)
{
ir = new IdentityRole(investigateRole);
var roleresult = roleManager.Create(ir);
}
var user = userManager.FindByName(name);
if (user == null)
{
user = new ApplicationUser { UserName = name, Email = name, EmailConfirmed = true };
var createUser = userManager.Create(user, pwd);
createUser = userManager.SetLockoutEnabled(user.Id, false);
}
// Add user admin to Role Admin if not already added
var rolesForUser = userManager.GetRoles(user.Id);
if (!rolesForUser.Contains("Admin"))
{
var result = userManager.AddToRole(user.Id, "Admin");
}
}
示例5: GetUsersCountInRole
public int GetUsersCountInRole(string role)
{
var roleStore = new RoleStore<IdentityRole>(this.dbContext);
var roleManager = new RoleManager<IdentityRole>(roleStore);
int count = roleManager.FindByName(role).Users.Count;
return count;
}
示例6: AddUserRole
public static void AddUserRole(string userName, string roleName)
{
using (var context = new ApplicationDbContext())
{
try
{
if (!context.Roles.Any(r => r.Name == roleName)) return;
var roleStore = new RoleStore<IdentityRole>(context);
var roleManager = new RoleManager<IdentityRole>(roleStore);
var store = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(store);
var user = userManager.FindByName(userName);
var role = roleManager.FindByName(roleName);
if (userManager.IsInRole(user.Id, role.Name)) return;
userManager.AddToRole(user.Id, role.Name);
context.SaveChanges();
}
catch (DbEntityValidationException ex)
{
// Retrieve the error messages as a list of strings.
// ReSharper disable once UnusedVariable
var errorMessages = ex.EntityValidationErrors
.SelectMany(x => x.ValidationErrors)
.Select(x => x.ErrorMessage);
throw;
}
}
}
示例7: GetAccountByPage
// GET: QLNews
public ActionResult GetAccountByPage(int? index, int page, int pageSize)
{
var roleManager = new RoleManager<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));
var idAdminRole = roleManager.FindByName("Admin").Id;
ApplicationUserManager userManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
GridViewModel<ApplicationUser> viewModel = new GridViewModel<ApplicationUser>();
int totalItems = userManager.Users.Where(x => x.Roles.Where(o => o.RoleId == idAdminRole).ToList().Count == 0).Count();
if (index != null && index != 0)
{
page = (int)Math.Ceiling((decimal)(index + 1) / (decimal)pageSize);
}
if (index == 0)
{
page = 1;
}
IEnumerable<ApplicationUser> data = userManager.Users.Where(x => x.Roles.Where(o => o.RoleId == idAdminRole).ToList().Count == 0).OrderBy(x => x.UserName).Skip((page - 1) * pageSize).Take(pageSize).ToList();
if (index != null)
{
viewModel = new GridViewModel<ApplicationUser> { GridData = data, CurrentPage = page, TotalItems = totalItems };
}
else
{
viewModel = new GridViewModel<ApplicationUser> { GridData = data, TotalItems = totalItems };
}
return Json(viewModel, JsonRequestBehavior.AllowGet);
}
示例8: GetUsersInRole
public List<User> GetUsersInRole(string roleName)
{
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));
var role = roleManager.FindByName(roleName).Users.First();
var usersInRole = db.Users.Where(u => u.Roles.Select(r => r.RoleId).Contains(role.RoleId)).ToList();
return usersInRole;
}
示例9: getRoleId
private string getRoleId(string n)
{
var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
var role = RoleManager.FindByName(n);
return role.Id;
}
示例10: CreateAdminUsers
public void CreateAdminUsers()
{
ApplicationDbInitializer.AddUser(dataContext);
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(dataContext));
IdentityRole admrole = roleManager.FindByName("SuperUser");
List<User> users = _userManager.Users.Where(u=>u.Roles.Any(r=>r.RoleId==admrole.Id)).ToList();
Assert.IsTrue(users.Count > 0);
}
示例11: CreateRole
public static void CreateRole(RoleManager<ApplicationRole> roleManager, string roleName)
{
var role = roleManager.FindByName(roleName);
if (role != null) return;
role = new ApplicationRole(roleName);
var roleResult = roleManager.Create(role);
}
示例12: SetupRolesAndUsers
private static void SetupRolesAndUsers(DbContext context)
{
var roleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(context));
var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));
// add roles
if (!roleManager.RoleExists(Role.Guest.ToString()))
roleManager.Create(new IdentityRole(Role.Guest.ToString()));
if (!roleManager.RoleExists(Role.Supplier.ToString()))
roleManager.Create(new IdentityRole(Role.Supplier.ToString()));
if (!roleManager.RoleExists(Role.Deactivated.ToString()))
roleManager.Create(new IdentityRole(Role.Deactivated.ToString()));
if (!roleManager.RoleExists(Role.User.ToString()))
roleManager.Create(new IdentityRole(Role.User.ToString()));
var adminRole = roleManager.FindByName(Role.Admin.ToString());
if (adminRole == null)
{
adminRole = new IdentityRole(Role.Admin.ToString());
roleManager.Create(adminRole);
}
#if DEBUG
//add admin user
var admin = userManager.Find(Admin_User, Admin_Pass);
if (admin == null)
{
admin = new ApplicationUser
{
UserName = Admin_User,
Email = Admin_Mail,
EmailConfirmed = true
};
var result = userManager.Create(admin, Admin_Pass);
// TODO: verify returned IdentityResult
userManager.AddToRole(admin.Id, Role.Admin.ToString());
result = userManager.SetLockoutEnabled(admin.Id, false);
}
var rolesForUser = userManager.GetRoles(admin.Id);
if (!rolesForUser.Contains(adminRole.Name))
{
var result = userManager.AddToRole(admin.Id, adminRole.Name);
}
//add normal user
if (userManager.Find("[email protected]", "1q2w3e4r") == null)
{
var user = new ApplicationUser
{
UserName = "[email protected]",
Email = "[email protected]",
EmailConfirmed = true
};
userManager.Create(user, "1q2w3e4r");
// TODO: verify returned IdentityResult
userManager.AddToRole(user.Id, Role.User.ToString());
}
#endif
}
示例13: Creat
public int Creat(ApplicationUser user)
{
ApplicationUserManager userManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
userManager.Create(user, user.Password);
userManager.AddToRole(userManager.FindByName(user.UserName).Id, "DangBai");
var roleManager = new RoleManager<Microsoft.AspNet.Identity.EntityFramework.IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));
var idAdminRole = roleManager.FindByName("Admin").Id;
int index = 0;
index = userManager.Users.Where(x => x.Roles.Where(o => o.RoleId == idAdminRole).ToList().Count == 0).OrderBy(x => x.UserName).ToList().IndexOf(user);
return index;
}
示例14: AdministratorMenu
public AdministratorMenu()
{
this.Text = "Administrator Menu";
var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));
var role = RoleManager.FindByName("Administrator");
var roleId = Guid.Parse(role.Id);
using (var db = new AromaContext())
{
var menuItems = db.SystemMenuList.Where(m => m.Active && m.RoleId.Equals(roleId)).Select(m=>m.SystemMenuListItem).OrderBy(m=>m.Text).ToArray();
foreach (var mnuItem in menuItems)
{
this.Add(mnuItem.Text, mnuItem.ActionName, mnuItem.ControllerName,mnuItem.Parameters);
}
}
//fixed items for now, could be fetched from DB at a later stage
/*
this.Add("System Accounts", "Index", "Account");
this.Add("Client Type", "Index", "ClientTypes");
this.Add("Ethnic Group", "Index", "EthnicGroups");
this.Add("Title", "Index", "Titles");
this.Add("Language", "Index", "Languages");
this.Add("Income Group", "Index", "IncomeGroups");
this.Add("Address Type", "Index", "AddressTypes");
this.Add("Province", "Index", "Provinces");
this.Add("Country", "Index", "Countries");
this.Add("Contact Type", "Index", "ContactTypes");
this.Add("Account Holder", "Index", "AccountHolders");
this.Add("Account Type", "Index", "AccountTypes");
this.Add("Bank", "Index", "Banks");
this.Add("Branch", "Index", "Branches");
this.Add("Contact Type", "Index", "ContactTypes");
this.Add("Financial Account","Index","finAccounts");
this.Add("Client Account", "Index", "finClientAccounts");
this.Add("Client Subscription", "Index", "ClientSubscriptions");
this.Add("Client", "Index", "Clients");
this.Add("Product", "Index", "Products");
this.Add("Subscription", "Index", "Subscriptions");
this.Add("Debit Order", "Index", "DebitOrders");
this.Add("Tickets", "Index", "SupportTickets");
this.Add("Ticket Templates", "Index", "SystemTicketTemplates");
this.Add("SMS", "Manage", "SystemSMS");
this.Add("SMS Templates", "Index", "SystemSMSTemplates");
this.Add("Postal Codes", "Index", "PostalCodes");*/
////////////////////////////////////////////////////////
//must always be under admin
if (this.Where(m => m.Text == "Menu Control").Count() == 0)
{
this.Add("Menu Control", "Index", "SystemMenuLists","");
}
/////////////////////////////////////////////////////////
}
示例15: Index
public ActionResult Index()
{
List<ApplicationUser> admins = new List<ApplicationUser>();
var RoleManager = new RoleManager<IdentityRole>(new RoleStore<IdentityRole>(new ApplicationDbContext()));
var userIds = RoleManager.FindByName("Admin").Users.Select(a => a.UserId).ToList();
foreach (var id in userIds)
{
admins.Add(UserManager.FindById(id));
}
var model = new IndexViewModel();
model.Admins = admins;
return View(model);
}