1

I'm using Form authentication in my mvc project.
I've created a CustomRoleProvider and implement two method:
IsUserInRole and 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; }
}

Also I added authorize attribute above my controller and actions like this:

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

But when a user request to access for actions that he doesn't have admin role, he can open it!

I run my project and set a break point in IsUserInRoles method, But it never fire! Where's the problem?

Majid Basirati
  • 2,665
  • 3
  • 24
  • 46
  • Explicitly try adding Assembly name `` – Hary Oct 02 '18 at 14:20
  • i did but that's not working, it doesn't matter what role you mention in this attribute, doesn't check roles at all. – Majid Basirati Oct 16 '18 at 04:59
  • i found out roles property in authorize is empty, why is that? – Majid Basirati Oct 16 '18 at 07:05
  • 1
    Have you installed the latest .NET Framework updates? I remember there was an issue with serialization and cookie sizes before, which was the main reason of `CustomRoleProvider` not being called. Turn on your Windows updates and install all of the missing and optional updates. – VahidN Nov 28 '18 at 05:36
  • @VahidN We have other projects with `CustomRoleProvider` that they works fine! Only this project has issue. I set breakpoints on `GetRolesForUser` and `IsUserInRole` and only `GetRolesForUser` execute but `IsUserInRole` never execute. – Majid Basirati Nov 28 '18 at 06:08

0 Answers0