I have a custom membership/roles provider that I use in my MVC controllers that I also want to have accessible to ASP.NET MVC, so I can use AuthorizationFilters, etc. Since so many people have implemented custom providers I imagine many people have done this but I haven't figured it out or found postings that address this problem specifically. This post is sort of a flip side of my question. In my case I have my custom provider working well with my controllers, and I want MVC to use it too.
我有一个自定义成员资格/角色提供程序,我在我的MVC控制器中使用,我也希望ASP.NET MVC可以访问,所以我可以使用AuthorizationFilters等。由于很多人已经实现了自定义提供程序,我想很多人都做过这个,但我没有想出来或发现专门解决这个问题的帖子。这篇文章有点像我的问题的另一面。在我的情况下,我的自定义提供程序与我的控制器配合良好,我也希望MVC也能使用它。
My provider is implemented with a IoC/dependency injection design. The provider exposes additional functionality beyond the baseline membership/roles API. In my controllers, I use Castle Windsor to create instances. The code looks similar to:
我的提供程序使用IoC /依赖注入设计实现。提供程序公开了基线成员资格/角色API之外的其他功能。在我的控制器中,我使用Castle Windsor创建实例。代码看起来类似于:
public class HomeController : Controller {
IMembershipService _membershipService;
public HomeController(IMembershipService membershipService) {
_membershipService= membershipService;
}
}
<castle>
<components>
<component id="MembershipService"
service="IMembershipService, MyApp"
type="MembershipService, MyApp" lifestyle="PerWebRequest">
<parameters>
<connectionString>#{defaultConnectionString}</connectionString>
</parameters>
</component>
</components>
</castle>
public class WindsorControllerFactory : DefaultControllerFactory {
private WindsorContainer _container;
public WindsorControllerFactory() {
_container = new WindsorContainer(new XmlInterpreter(new ConfigResource("castle")));
List<Type> controllerTypes = new List<Type>();
foreach (Type t in Assembly.GetExecutingAssembly().GetTypes()) {
if (typeof(IController).IsAssignableFrom(t))
controllerTypes.Add(t);
}
foreach (Type t in controllerTypes) {
// LifestyleType.Transient = new controller instance for each request
_container.AddComponentLifeStyle(t.FullName, t, LifestyleType.Transient);
}
}
protected override IController GetControllerInstance(Type controllerType) {
return (IController)_container.Resolve(controllerType);
}
This all works great in my C# code but I want to wire my provider into MVC to use [Authorize] filters with it:
这一切都在我的C#代码中运行良好但我想将我的提供程序连接到MVC以使用它的[Authorize]过滤器:
[Authorize (Users="user1, user2", Roles="role8")]
public ViewResult MyResult(int x) {
// implement
}
I know that the usual way to tell ASP.NET about a custom membership or roles provider is in the web.config file as below but if I do this ASP.NET will just try to call the default constructor, which won't work. Any help appreciated.
我知道告诉ASP.NET有关自定义成员资格或角色提供程序的常用方法是在web.config文件中,如下所示,但如果我这样做,ASP.NET将只尝试调用默认构造函数,这将无效。任何帮助赞赏。
<membership>
<providers>
<clear/>
<add name="MyMembershipProvider" type="MyMembershipProvider">
</providers>
</membership>
1 个解决方案
#1
24
The simplest way to get this to work is to use ASP.NET's standard mechanism of <membership>
in web.config. You just let it use the default constructor but you override Initialize() and pull the dependencies there. Use this as reference.
让它工作的最简单方法是在web.config中使用ASP.NET的
Personally, due to things like this, I prefer to avoid the provider model altogether so I use an approach similar to the ones described in the MonoRail docs. IMHO it's less bloated and more flexible. In the end, it's just about setting HttpContext.User with a proper IPrincipal implementation which is what the AuthorizeAttribute uses.
就个人而言,由于这样的事情,我更愿意完全避免使用提供者模型,所以我使用的方法类似于MonoRail文档中描述的方法。恕我直言,它不那么臃肿,更灵活。最后,它只是使用适当的IPrincipal实现设置HttpContext.User,这是AuthorizeAttribute使用的。
I recently blogged about a solution to do proper IoC with MembershipProviders.
我最近写了一篇关于使用MembershipProviders做适当IoC的解决方案。
#1
24
The simplest way to get this to work is to use ASP.NET's standard mechanism of <membership>
in web.config. You just let it use the default constructor but you override Initialize() and pull the dependencies there. Use this as reference.
让它工作的最简单方法是在web.config中使用ASP.NET的
Personally, due to things like this, I prefer to avoid the provider model altogether so I use an approach similar to the ones described in the MonoRail docs. IMHO it's less bloated and more flexible. In the end, it's just about setting HttpContext.User with a proper IPrincipal implementation which is what the AuthorizeAttribute uses.
就个人而言,由于这样的事情,我更愿意完全避免使用提供者模型,所以我使用的方法类似于MonoRail文档中描述的方法。恕我直言,它不那么臃肿,更灵活。最后,它只是使用适当的IPrincipal实现设置HttpContext.User,这是AuthorizeAttribute使用的。
I recently blogged about a solution to do proper IoC with MembershipProviders.
我最近写了一篇关于使用MembershipProviders做适当IoC的解决方案。