【发布时间】: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