如何整合与ASP.NET MVC国际奥委会成员资格提供 [英] How to integrate IoC Membership provider with ASP.NET MVC

查看:109
本文介绍了如何整合与ASP.NET MVC国际奥委会成员资格提供的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我在我的MVC控制器,我也希望有ASP.NET MVC访问使用自定义成员资格/角色提供商,这样我就可以用AuthorizationFilters等。由于很多人已经实现了自定义的供应商我想很多人已经这样做了,但我还没有想通了,或发现专门解决这个问题的帖子。 <一href=\"http://stackoverflow.com/questions/922891/inject-a-membershipprovider-into-asp-net-mvc-accountcontroller\">This帖子是那种​​我的问题的另一面的。在我来说,我有我的自定义提供与我的控制器运作良好,我想MVC使用它了。

我的供应商与IOC /依赖注入的设计来实现。提供者暴露超越基准会员/角色API附加功能。在我的控制器,我使用温​​莎城堡创建实例。在code类似于:

 公共类HomeController的:控制器{
    IMembershipService _membershipService;
    公众的HomeController(IMembershipService membershipService){
        _membershipService = membershipService;
    }
}&LT;&城堡GT;
 &LT;成分&gt;
  &lt;成分ID =MembershipService
             服务=IMembershipService,MyApp的
             键入=MembershipService,MyApp的生活方式=PerWebRequest&GT;
    &LT;&参数GT;
      &LT;&的connectionString GT;#{defaultConnectionString}&LT; /&的connectionString GT;
    &LT; /参数&GT;
  &LT; /成分&gt;
 &LT; /成分&gt;
&LT; /城堡&GT;公共类WindsorControllerFactory:DefaultControllerFactory {
    私人WindsorContainer _container;
    公共WindsorControllerFactory(){
        _container =新WindsorContainer(新XmlInter preTER(新ConfigResource(城堡)));        清单&LT;类型&GT; controllerTypes =新的List&LT;类型和GT;();
        的foreach(类型吨Assembly.GetExecutingAssembly()。GetTypes()){
            如果(typeof运算(一个IController).IsAssignableFrom(T))
                controllerTypes.Add(T);
        }        的foreach(T型在controllerTypes){
            为每个请求// LifestyleType.Transient =新的控制器实例
            _container.AddComponentLifeStyle(t.FullName,T,LifestyleType.Transient);
        }
    }    保护覆盖一个IController GetControllerInstance(类型controllerType){
        回报(一个IController)_container.Resolve(controllerType);
    }

这所有的作品在我的C#code伟大的,但我想我的电汇到供应商的MVC使用[授权]过滤器与它:

  [授权(用户=用户1,用户2,角色=role8)]
公众的ViewResult MyResult(INT X){
    // 实行
}

我知道,通常的方式告诉ASP.NET有关自定义成员资格或角色提供商中,如下所示的web.config文件,但如果我这样做ASP.NET只是尝试调用默认的构造函数,从而赢得了将不起作用。任何帮助AP preciated。

 &LT;会员和GT;
 &LT;供应商&GT;
  &LT;清/&GT;
  &LT;添加名称=MyMembershipProviderTYPE =MyMembershipProvider&GT;
 &LT; /供应商&GT;
&LT; /会员&GT;


解决方案

要得到这个工作,最简单的方法是使用ASP.NET的标准机制&LT;会员&GT; 在web.config中。你就让它使用默认的构造函数的您覆盖初始化()和的依赖那里。使用<一个href=\"http://$c$c.google.com/p/webdotnet/source/browse/trunk/Steeg.Framework/Web/Security/?r=2\">this作为参考。

就个人而言,由于这样的事情,我preFER避免提供者模型干脆所以我用类似的人的在单轨文档描述。恕我直言,这是不太臃肿,更灵活。最后,它只是设置 HttpContext.User中一个合适<一href=\"http://msdn.microsoft.com/en-us/library/system.security.principal.iprincipal.aspx\">IPrincipal实施这是什么AuthorizeAttribute使用。

我最近博客中提到的解决方案做适当的IoC和MembershipProviders 的。

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.

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:

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:

[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.

<membership>
 <providers>
  <clear/>
  <add name="MyMembershipProvider" type="MyMembershipProvider">
 </providers>
</membership>

解决方案

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.

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.

I recently blogged about a solution to do proper IoC with MembershipProviders.

这篇关于如何整合与ASP.NET MVC国际奥委会成员资格提供的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆