ASP.NET MVC Multiple Custom AuthorizeAttribute

时间:2012-09-04 14:15:38

标签: asp.net asp.net-mvc asp.net-mvc-3 asp.net-membership

我正在使用ASP.NET MVC 4开发一个Web应用程序,我创建了一些自定义MembershipProvider和RoleProvider,如下所示:

# Custom MembershipProviders
<membership defaultProvider="myMembershipProvider">
  <providers>
    <clear />
    <add name="myMembershipProvider" type="WebApp1.Business.Auth.MyMembershipProvider" />
    <add name="adminMembershipProvider" type="WebApp1.Business.Auth.AdminMembershipProvider" />
  </providers>
</membership>


# Custom RoleProviders
<roleManager enabled="true" defaultProvider="MyRoleProvider">
  <providers>
    <clear />
    <add name="MyRoleProvider" type="WebApp1.Business.Auth.MyRoleProvider" />
    <add name="AdminRoleProvider" type="WebApp1.Business.Auth.AdminRoleProvider" />
  </providers>
</roleManager>

我的目的是创建一个名为Logon的自定义AuthorizeAttribute,它将使用“myMembershipProvider”和“MyRoleProvider”;并创建另一个名为AdminLogon的将使用“adminMembershipProvider”和“AdminRoleProvider”。

我创建两个自定义AuthorizeAttributes的原因是因为我想分离一些获取每个角色权限的方法。

问题是,如何动态设置默认的MembershipProvider和RoleProviders 在自定义AuthorizeAttribute中。

例如:

// Set the default MembershipProvider as adminMembershipProvider
// Set the default RoleProvidersas as AdminRoleProvider
[AdminLogon(Roles = "Administrador")]
public ActionResult Funcionarios()
{
    return View();
}


// Set the default MembershipProvider as myMembershipProvider
// Set the default RoleProvidersas as MyRoleProvider
[Logon(Roles = "Administrador")]
public ActionResult Funcionarios()
{
    return View();
}

0 个答案:

没有答案