【发布时间】:2015-05-29 09:17:14
【问题描述】:
我对 Owin 及其上下文有一个奇怪的问题。我有一个看起来像这样的控制器操作
[HttpPost]
public ActionResult Login(string username, string password)
{
var ctx = HttpContext.GetOwinContext();
// code omitted
return new HttpUnauthorizedResult();
}
如果我在var ctx 处设置断点并调试我的应用程序,则该变量将获得其值。但是如果只是正常启动应用程序并使用 IIS Express (ctrl+f5) 运行它,应用程序会在 var ctx 处中断并给我错误。
“在上下文中找不到 owin.Environment 项。”
是什么导致了这个错误?
我在 Visual Studio 2013 .Net 版本 4.5.1 中运行它。感谢您的帮助!
如果您认为需要更多来自 startup.cs 或其他任何地方的代码,请告诉我。
编辑:这是我的启动配置。
[assembly: OwinStartup(typeof(XXX.Startup))]
namespace XXX
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
}
}
namespace XXX
{
public partial class Startup
{
public void ConfigureAuth(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = AuthenticationTypes.Cookie,
AuthenticationMode = AuthenticationMode.Active,
SlidingExpiration = true,
ExpireTimeSpan = new TimeSpan(0, 30, 0),
});
app.UseMyAuthentication(new MyAuthenticationOptions()
{
AuthenticationType = AuthenticationTypes.MyAuthType,
SignInAsAuthenticationType = AuthenticationTypes.Cookie,
AuthenticationMode = AuthenticationMode.Passive,
CallbackPath = new PathString("/authorization/callback")
});
}
}
}
【问题讨论】:
-
看this线程。
-
添加了一些配置。 @YuvalItzchakov,它找到了启动类,但是在不执行 ctrl+f5 时获取上下文时会抛出异常,但在执行 f5 并通过 getOwinContext 方法调试时不会。
标签: c# asp.net-mvc owin katana