简体   繁体   中英

Roles not working in ActiveDirectoryMembershipProvider

I have a web-site which uses forms auth and ActiveDirectoryMembershipProvider. I have an Action in controller like this:

[Authorize(Roles = "jira-developers")]
        [HttpGet]
        public ActionResult MonitorForm()
        {
            var list = Dal.GetActualData();
            return View(list);
        }

I'm totally sure that my user is in group with Name="jira-developers", but auth fails. If i remove Roles parameter, the auth will work fine.

What am i doing wrong? I'll be gratefull for any help!

As nobody gave me an answer i'll answer this question myself. ActiveDirectoryMembershipProvider can only handle auth and to enable roles management i had to specify rolesManager. I implemented my own RoleProvider (because i need some specific functionality) and now my Web.Config looks like this:

<system.web>
<authentication mode="Forms">
  <forms name=".ADAuthCookie" loginUrl="~/Account/Login" timeout="45" slidingExpiration="false" protection="All" />
</authentication>
<membership defaultProvider="ADMembershipProvider">
  <providers>
    <clear />
    <add name="ADMembershipProvider"
         type="System.Web.Security.ActiveDirectoryMembershipProvider"
         connectionStringName="ADConnectionString"
         attributeMapUsername="sAMAccountName" />
  </providers>
</membership>
<roleManager enabled="true" defaultProvider="AdRoleProvider">
  <providers>
    <clear/>
    <add name="AdRoleProvider" type="InternalAutomation.Providers.AdRoleProvider"/>
  </providers>
</roleManager>
<compilation debug="true" targetFramework="4.5.1" />
<httpRuntime targetFramework="4.5.1" />

Do you have the role defined in your Roles table? (Depending on the version you're using the table could be named slightly different than my screen shot below)

数据库表中的角色

You should have an entry with a 'RoleName' of "jira-developers". The user hitting the action should also have an entry in the '...UsersInRoles' table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM