将 MembershipProvider 注入 ASP.NET MVC AccountController

发布于 2024-07-23 06:07:34 字数 2385 浏览 1 评论 0原文

ASP.NET MVC 1.0 项目模板包含一个 AccountController 类,它支持构造函数注入:

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

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

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

我确信很多你们中的一些人已经使用它们进行单元测试,但我的目标是使用 Windsor 注入 SqlMembershipProvider,从而在运行时使用 Windsor XML 文件而不是 web.config 对其进行配置。 换句话说,我想对 AccountMembershipService 类使用构造函数注入,并且我想继续使用内置的 ASP.NET 2.0 会员系统。 我只想通过 Windsor IoC 来配置会员系统。

这是否可以在不编写我自己的 MembershipProvider 的情况下实现,或者 SqlMembershipProvider 不能很好地与 IoC 配合使用?

来自 MSDN:“ASP.NET 调用 SqlMembershipProvider 构造函数来创建应用程序配置中指定的 SqlMembershipProvider 类的实例。此构造函数不适合在您的代码中使用。”< /p>

我相信 菲尔问了一个非常类似的问题,这里是 他收到的答案

感谢您的帮助。


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 设计的使用中。

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();
}

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

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

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

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

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

I believe Phil asked a very similar question, here are the answers he received.

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 writes about this technique. I am just trying to integrate SqlMembershipProvider into his use of this IoC design.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后来的我们 2024-07-30 06:07:34

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

<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 选择适当的提供商,如下所示:

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)

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>

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文