#.net #asp.net-mvc #roleprovider
#.net #asp.net-mvc #поставщик ролей
Вопрос:
Я пытаюсь реализовать пользовательский поставщик ролей, но, похоже, он не вызывается:
CustoRoleProvider:
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace DBSv1.Application { public class CustomRoleProvider : System.Web.Security.RoleProvider { public CustomRoleProvider() { } public override string ApplicationName { get =gt; throw new NotImplementedException(); set =gt; throw new NotImplementedException(); } public override void AddUsersToRoles(string[] usernames, string[] roleNames) { throw new NotImplementedException(); } public override void CreateRole(string roleName) { throw new NotImplementedException(); } public override bool DeleteRole(string roleName, bool throwOnPopulatedRole) { throw new NotImplementedException(); } public override string[] FindUsersInRole(string roleName, string usernameToMatch) { throw new NotImplementedException(); } public override string[] GetAllRoles() { throw new NotImplementedException(); } public override string[] GetRolesForUser(string username) { // Implement the logic to resolve the user's name using (var db = new DBSv1.Models.nhsdmsEntities()) { var rolePermissionManager = new RolePermissionManager(db); return rolePermissionManager.ResolveRoleName(username); } } public override string[] GetUsersInRole(string roleName) { throw new NotImplementedException(); } public override bool IsUserInRole(string username, string roleName) { string[] roles = GetRolesForUser(username); foreach (string role in roles) { if (roleName.Equals(role, StringComparison.OrdinalIgnoreCase)) { return true; } } return false; } public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames) { throw new NotImplementedException(); } public override bool RoleExists(string roleName) { throw new NotImplementedException(); } } }
RolePermissionManager:
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace DBSv1.Application { public class RolePermissionManager { private DBSv1.Models.nhsdmsEntities db; public RolePermissionManager(DBSv1.Models.nhsdmsEntities context) { db = context; } public string[] ResolveRoleName(string username) { // Retrieve user's information from the database Models.tblUser foundUser = (from user in db.tblUsers where user.Username == username select user).SingleOrDefault(); if (foundUser == null) { return new string[0]; } // Return corresponding roles if (foundUser.IsAdmin ?? false) { return new[] { "Admin" }; } else if (foundUser.IsAdd ?? false) { return new[] { "Add" }; } return new string[0]; } } }
web.config
:
lt;system.webgt; lt;authentication mode="Windows" /gt; lt;authorizationgt; lt;deny users="?" /gt; lt;/authorizationgt; lt;roleManager defaultProvider="CustomRoleProvider" enabled="true" cacheRolesInCookie="true"gt; lt;providersgt; lt;clear /gt; lt;add name="CustomRoleProvider" type="DBSv1.Application.CustomRoleProvider" /gt; lt;/providersgt; lt;/roleManagergt;
Can anyone see what I’m missing?
// GET: Room [Authorize(Roles = "Admin")] public ActionResult Index() { ViewData["LoginType"] = "DBS Admin"; return View(new Models.nhsdmsEntities().tblRooms.ToList()); }