C# 如何从Global.asax获取OwinContext?
我正在尝试设置依赖项注入,我需要将C# 如何从Global.asax获取OwinContext?,c#,asp.net-mvc,owin,simple-injector,C#,Asp.net Mvc,Owin,Simple Injector,我正在尝试设置依赖项注入,我需要将iaauthenticationmanager从ASP.NET标识注入到OwinContext 为此,我从我的Global.asax->ServiceConfig.Configure()运行: container.Register(() => HttpContext.Current.GetOwinContext().Authentication); 但当我运行我的应用程序时,我会收到以下消息: 在上下文中未找到owin.Environment项 为什么
iaauthenticationmanager
从ASP.NET标识注入到OwinContext
为此,我从我的Global.asax->ServiceConfig.Configure()
运行:
container.Register(() => HttpContext.Current.GetOwinContext().Authentication);
但当我运行我的应用程序时,我会收到以下消息:
在上下文中未找到owin.Environment项
为什么这个HttpContext.Current.GetOwinContext()在Global.asax中不可用
Startup.cs
Startup.Auth.cs
公共部分类启动
{
public void ConfigureAuth(IAppBuilder应用程序)
{
app.UseCookieAuthentication(新的CookieAuthenticationOptions
{
AuthenticationType=DefaultAuthenticationTypes.ApplicationOkie,
LoginPath=新路径字符串(“/Account/Login”),
Provider=新CookieAuthenticationProvider
{
OnValidateIdentity=SecurityStampValidator.OnValidateIdentity(
validateInterval:TimeSpan.FromMinutes(30),
regenerateIdentityCallback:(manager,user)=>manager.CreateIdentityAsync(user,DefaultAuthenticationTypes.ApplicationOkie),
getUserIdCallback:(id)=>(Int32.Parse(id.GetUserId())
)
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
}
}
我用以下方法解决了这个问题:
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.isVerification()
出现在SimpleInjector.Advanced
查看。主要是在Startup.csHm之前调用Global.asax,我的目录中确实有“Microsoft.Owin.Host.SystemWeb.dll”。在我配置了auth(…)之后,我尝试将所有内容从我的Global.asax移动到Startup.cs,但仍然会出现相同的消息。我只是尝试创建一个全新的示例应用程序,并尝试获取具有相同结果的OwinContext。必须有一种方法:-)关于使用简单的注射器的Owin。也许这会有帮助。谢谢@Steven,我到家后会尽力让你知道的。:-)这是什么“registerWebRequest()”?你从哪里得到的?Unity似乎没有这样的方法或扩展。您正在使用SimpleInjector。好啊问题是如何注册该类型。例如,控制器需要一个repo,它需要IauthManager。有人知道如何使用Castle Windsor执行此操作吗?在Castle Windsor中,您可以执行以下操作: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();
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);
}
}
container.RegisterPerWebRequest(() =>
{
if (HttpContext.Current != null && HttpContext.Current.Items["owin.Environment"] == null && container.IsVerifying())
{
return new OwinContext().Authentication;
}
return HttpContext.Current.GetOwinContext().Authentication;
});