【问题标题】:How to get OwinContext from Global.asax?如何从 Global.asax 获取 OwinContext?
【发布时间】:2014-08-30 20:59:06
【问题描述】:

我正在尝试设置我的依赖注入,我需要将 ASP.NET 标识中的 IAuthenticationManager 注入到 OwinContext

为此,我来自我的Global.asax -> ServiceConfig.Configure() 跑步:

 container.Register(() => HttpContext.Current.GetOwinContext().Authentication);

但是当我运行我的应用程序时,我会收到以下消息:

在上下文中找不到 owin.Environment 项

为什么 Global.asax 没有这个 HttpContext.Current.GetOwinContext()?

Startup.cs

[assembly: OwinStartupAttribute(typeof(MyApp.Web.Startup))]
namespace Speedop.Web
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
        }
    }
}

Startup.Auth.cs

public partial class Startup
{
    public void ConfigureAuth(IAppBuilder app)
    {
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<UserManager<User, int>, User, int>(
                    validateInterval: TimeSpan.FromMinutes(30),
                    regenerateIdentityCallback: (manager, user) => manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie),
                    getUserIdCallback: (id) => (Int32.Parse(id.GetUserId()))
                    )
            }
        });

        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
    }
}

【问题讨论】:

  • 看看this answer。主要是在 Startup.cs 之前调用 Global.asax
  • 嗯,我的目录中确实有“Microsoft.Owin.Host.SystemWeb.dll”。在执行 ConfigureAuth(...) 之后,我尝试将所有内容从 Global.asax 移动到 Startup.cs,但仍然出现相同的消息。
  • 我刚刚尝试创建一个全新的示例应用程序并尝试获取具有相同结果的 OwinContext。一定有办法:-)
  • Here's a discussion 关于 Owin 与 Simple Injector。也许这有帮助。
  • 谢谢@Steven,我回家后会尝试并告诉你。 :-)

标签: c# asp.net-mvc owin simple-injector


【解决方案1】:

我用以下方法解决了这个问题:

container.RegisterPerWebRequest(() =>
{
    if (HttpContext.Current != null && HttpContext.Current.Items["owin.Environment"] == null && container.IsVerifying())
    {
        return new OwinContext().Authentication;
    }
    return HttpContext.Current.GetOwinContext().Authentication;

});

似乎 OwnContext 在启动时不存在,所以我会等待它并在它存在时注入它。请注意container.IsVerifying() 存在于SimpleInjector.Advanced

【讨论】:

  • 这是什么“RegisterPerWebRequest()”?你从哪里得到它? Unity似乎没有这样的方法或扩展
  • 找到了。您正在使用 SimpleInjector。好的。问题是您如何注册类型。例如,控制器需要一个 repo,它需要 IauthManager。
  • 有人知道温莎城堡怎么做吗?
  • 在温莎城堡中,您可以执行以下操作: container.Register(Component.For().UsingFactoryMethod( () => { if (HttpContext.Current != null && HttpContext.Current. Items["owin.Environment"] == null) { return new OwinContext().Authentication; } return HttpContext.Current.GetOwinContext().Authentication; }).LifestylePerWebRequest());
猜你喜欢
  • 2014-07-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-03
  • 2012-10-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多