我想建立我的依賴注入,我需要注入一個IAuthenticationManager
從ASP.NET身份到OwinContext
。如何從Global.asax獲取OwinContext?
對於這個我是從我的Global.asax -> ServiceConfig.Configure()
運行:
container.Register(() => HttpContext.Current.GetOwinContext().Authentication);
但是,當我運行我的應用程序得到這個消息:
沒有owin.Environment項目是在上下文中找到
爲什麼這個HttpContext.Current.GetOwinContext()不可用Global.asax?
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);
}
}
看看[這個答案](http://stackoverflow.com/questions/20168978/do-i-need-a-global-asax-cs-file-at-all -if-IM-使用-AN-owin-啓動-CS-類和)。主要是,Global.asax之前調用Startup.cs – Jonesopolis
嗯,我的'Microsoft.Owin.Host.SystemWeb.dll'在我的目錄中。我嘗試將所有內容從我的Global.asax移動到Startup.cs後,我執行了ConfigureAuth(...),但仍然出現了相同的消息。 – janhartmann
我剛剛嘗試創建一個全新的示例應用程序,並試圖獲得相同結果的OwinContext。必須有一個方法:-) – janhartmann