了解Autofac中间件与ASP的使用.NET Webforms, OWIN和ASP.净的身份

本文关键字:ASP OWIN 身份 Webforms 中间件 Autofac 了解 NET | 更新日期: 2023-09-27 18:14:41

我目前正在更新一个旧的ASP。使用MembershipProvider和RoleProvider的. NET Webforms应用程序,以使用较新的ASP。. NET标识类。此外,我使用Autofac进行依赖注入,为了一致性,我希望能够在新的Identity类中使用它。

ASP。. NET Identity现在依赖于OWIN中间件,我已经成功地通过添加以下Startup.cs类将OWIN中间件设置合并到我的应用程序中:

internal partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        var builder = new ContainerBuilder();
        AutofacConfig.RegisterTypes(builder);
        AutofacConfig.RegisterIdentityTypes(builder, app);
        var container = builder.Build();
        Global.SetContainer(container);
        app.UseAutofacMiddleware(container);
        ConfigureAuth(app);
    }
}

我像这样在Autofac中注册身份类:

internal static class AutofacConfig
{      
    public static void RegisterIdentityTypes(ContainerBuilder builder, IAppBuilder app)
    {
        builder.RegisterType<ApplicationUserStore>().As<IUserStore<ApplicationUser>>().InstancePerRequest();
        builder.RegisterType<ApplicationUserManager>().AsSelf().InstancePerRequest();
        builder.RegisterType<ApplicationSignInManager>().AsSelf().InstancePerRequest();
        builder.Register(c => HttpContext.Current.GetOwinContext().Authentication).InstancePerRequest();
        builder.Register(c => app.GetDataProtectionProvider()).InstancePerRequest();
    }
}

我可以看到,当我导航到我的WebForms sign in页面以下ApplicationSignInManager类被实例化,它的构造函数参数也被解析,所以这表明我Autofac工作正常。

public class ApplicationSignInManager : SignInManager<ApplicationUser, string>
{
    public ApplicationSignInManager(
        ApplicationUserManager userManager,
        IAuthenticationManager authenticationManager)
        : base(userManager, authenticationManager)
    {
        // userManager is valid
        // authenticationManager is valid
    }
}

当我点击WebForms登录页面上的登录按钮时,下面的方法被调用,ApplicationSignInManager通过Autofac属性注入实例化。然后向下调用正确的Identity类实现。

public partial class SignIn : BasePage
{
    // Autofac property injection 
    public ApplicationSignInManager ApplicationSignInManager { get; set; }
    ....
    protected void SignInClick(object sender, EventArgs e)
    {
        // Validate the user password
        var result = this.ApplicationSignInManager.PasswordSignIn(
            this.email.Text,
            this.password.Text,
            this.remember.Checked,
            true);
        ....
     }

所以一切看起来都很好,但有些事情对我来说似乎不太对,我看不出OWIN和autoface在哪里连接,甚至是必需的。如果我从Startup.cs中删除以下行,则为该火焰添加燃料…

    app.UseAutofacMiddleware(container);

…一切都照常工作!这不可能是对的,对吧?

MVC的许多例子似乎表明这是获得Identity对象的正确方法(通过GetOwinContext()调用):

    protected void SignInClick(object sender, EventArgs e)
    {
        var signInManager = this.Context.GetOwinContext().GetUserManager<ApplicationSignInManager>();
        // Validate the user password
        var result = signInManager.PasswordSignIn(
            this.email.Text,
            this.password.Text,
            this.remember.Checked,
            true);
        ....
    }

但是在执行该代码时,'signInManager'变量总是NULL。

我真的需要这行代码吗?

 app.UseAutofacMiddleware(container);
谁能给我指个正确的方向?

了解Autofac中间件与ASP的使用.NET Webforms, OWIN和ASP.净的身份

文档说明

对于一个简单的场景,app.UseAutofacMiddleware(container);将处理在OWIN请求范围内添加Autofac生命周期以及在管道中添加Autofac注册的中间件。

见来源确认。

基本上,如果你从autoface注入任何东西到OWIN管道(即中间件),你需要这一行。但是对于您的场景,它看起来不像您那样做任何事情,因此删除这一行不会为您改变任何东西。

至于从OWIN管道解析对象,你真的不需要这么做——你已经从autofacc解析对象了。但是,SecurityStampValidator仍然从OWIN中解析ApplicationUserManager,因此您仍然需要在OWIN中注册它:

app.CreatePerOwinContext(() => DependencyResolver.Current.GetService<ApplicationUserManager>());

看这个问题/答案背后的原因

我知道这是相当晚的游戏,但我正在更新一个传统的应用程序,我遇到了"IAppBuilder不包含'UseAutofacMiddleware'"定义错误,并通过添加NuGet包Autofac.Owin.

修复它