IsUserInRole не выполняется в моем CustomRoleProvider - PullRequest
0 голосов
/ 02 октября 2018

Я использую аутентификацию по форме в моем проекте mvc.
Я создал CustomRoleProvider и реализую два метода:
IsUserInRole и GetRolesForUser

Web.config

<roleManager defaultProvider="CustomRoleProvider" enabled="true">
  <providers>
    <clear />
    <add name="CustomRoleProvider"  type="SASS.UI.CustomRoleProvider" connectionStringName="AfterSaleConnection"  />
  </providers>
</roleManager>

FilterConfig

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    ...
    filters.Add(new AuthorizeAttribute());
}

CustomRoleProvider

public class CustomRoleProvider : RoleProvider
{
    private readonly IUserService _userServices;


    public CustomRoleProvider()
    {

        this._userServices = new UserServices(new Context());
    }
    public override bool IsUserInRole(string username, string roleName)
    {

        var user = _userServices.GetUser(username.GetUserId());

        if (user.IsAdmin)
            return true;

        return user.UserAccess.Any(y => (y.Role.ToString().ToLower() == roleName.ToLower()));
    }

    public override string[] GetRolesForUser(string username)
    {
        var user = _userServices.GetUser(username.GetUserId());

        List<string> accessList = new List<string>();

        if (user.UserAccess.Any(x => x.Role == Access.Admin))
        {
            foreach (Access access in Enum.GetValues(typeof(Access)))
            {

                accessList.Add(access.ToString().ToLower());
            }
            return accessList.ToArray();
        }
        var roles= user.UserAccess.Select(x => x.Role.ToString().ToLower()).ToArray();
        return roles;
    }

    public override void CreateRole(string roleName)
    {
        throw new NotImplementedException();
    }

    public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
    {
        throw new NotImplementedException();
    }

    public override bool RoleExists(string roleName)
    {
        throw new NotImplementedException();
    }

    public override void AddUsersToRoles(string[] usernames, string[] roleNames)
    {
        throw new NotImplementedException();
    }

    public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
    {
        throw new NotImplementedException();
    }

    public override string[] GetUsersInRole(string roleName)
    {
        throw new NotImplementedException();
    }

    public override string[] GetAllRoles()
    {
        throw new NotImplementedException();
    }

    public override string[] FindUsersInRole(string roleName, string usernameToMatch)
    {
        throw new NotImplementedException();
    }

    public override string ApplicationName { get; set; }
}

Также я добавил authorizeатрибут над моим контроллером и действия, подобные этому:

[Authorize(Roles = "admin")]
public virtual ActionResult List()
{
    return View("CustomerList");
}

Но когда пользователь запрашивает доступ для действий, для которых у него нет роли admin, он может открыть его!

Я запускаю свой проект и устанавливаю точку останова в методе IsUserInRoles, но он никогда не срабатывает!Где проблема?

...