将会员资格注入ASP.Net MVC AccountController [英] Inject a MembershipProvider into ASP.Net MVC AccountController

本文介绍了将会员资格注入ASP.Net MVC AccountController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Asp.Net MVC 1.0项目模板包括一个支持构造函数注入的 AccountController 类:

Asp.Net MVC 1.0 project templates include an AccountController class, which supports constructor injection:

public AccountController(IFormsAuthentication formsAuth, 
    IMembershipService service)
{
    FormsAuth = formsAuth ?? new FormsAuthenticationService();
    MembershipService = service ?? new AccountMembershipService();
}

还包括一个 AccountMembershipService 类,它也支持构造函数注入:

An AccountMembershipService class is also included, and it too supports constructor injection:

public AccountMembershipService(MembershipProvider provider)
{
    _provider = provider ?? Membership.Provider;
}

我确信很多人都用这些单元测试,但是目标是使用Windsor注入 SqlMembershipProvider ,因此可以在运行时使用Windsor xml文件而不是web.config进行配置。换句话说,我想实际使用AccountMembershipService类的构造函数注入,我想继续使用内置的ASP.Net 2.0成员资格系统。我只想让会员系统的配置通过Windsor IoC。

I'm sure many of you have used these for unit testing, but my goal is to inject a SqlMembershipProvider using Windsor, and thus configure it at runtime using Windsor xml files rather than web.config. In other words, I want to actually use constructor injection for the AccountMembershipService class, and I want to continue using the built in ASP.Net 2.0 Membership system. I just want the configuration of the Membership system to go through Windsor IoC.

这可能没有写我自己的MembershipProvider,还是SqlMembershipProvider不能很好地与IoC播放?

Is this possible without writing my own MembershipProvider, or does SqlMembershipProvider not play well with IoC?


从MSDN :ASP.NET调用SqlMembershipProvider构造函数以创建应用程序配置中指定的SqlMembershipProvider类的实例,该构造函数

From MSDN: "The SqlMembershipProvider constructor is called by ASP.NET to create an instance of the SqlMembershipProvider class as specified in the configuration for the application. This constructor is not intended to be used from your code."

我相信 Phil问了一个非常相似的问题,但我是没有足够的洞察力他收到的答案

I believe Phil asked a very similar question, but I'm not experienced enough to grok the answers he received.

感谢您的帮助。



更新:
要清楚,我通过DI提供应用程序MembershipProvider的原因是支持多个租户。每个租户都有一个隔离的数据库,其中包含 ASP会员资格表。 DI允许我在运行时切换连接字符串,从而使核心应用程序不知道每个租户使用哪个数据库。 Windsor正在控制DI,它知道租户通过url提出请求:

Thanks for your help.


UPDATE: Just to be clear, my reason for suppling the application's MembershipProvider via DI is to support multiple tenants. Each tenant has an isolated database with ASP membership tables. DI allows me to switch connection strings at runtime and thus keep the core application agnostic about which database is being used for each tenant. Windsor is controlling the DI and it "knows" which tenant makes the request via url:

var url = HttpContext.Current.Request.ServerVariables["HTTP_HOST"]

Mike Hadlow 在这里谈论这个技术我只是想将SqlMembershipProvider集成到他使用这个IoC设计中。

Mike Hadlow talks about this technique here. I am just trying to integrate SqlMembershipProvider into his use of this IoC design.

再次感谢

推荐答案

假设您的会员提供商配置如下:

Assuming you have your membership providers configured something like this:

<membership>
    <providers>
        <clear/>
        <add name="www.tenant1.com" 
         type="System.Web.Security.SqlMembershipProvider, ..." 
         .../>
        <add name="www.tenant2.com" 
         type="System.Web.Security.SqlMembershipProvider, ..." 
         .../>
    </providers>
</membership>

您可以让Windsor选择适当的提供商,如下所示:

you can have Windsor select the appropriate provider like this:

var container = new WindsorContainer();
container.AddFacility<FactorySupportFacility>();
container.Register(Component.For<MembershipProvider>()
    .LifeStyle.Transient
    .UsingFactoryMethod(() => Membership.Providers[HttpContext.Current.Request.Url.Host]));
... (your controller registrations, etc)

这篇关于将会员资格注入ASP.Net MVC AccountController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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