Asp.net HttpContext.Current.Session为空+;奥温
我对OWIN完全陌生,这个问题一直是我的主要障碍 基本上,在我的MVC应用程序中,我在启动课程中有以下内容:Asp.net HttpContext.Current.Session为空+;奥温,asp.net,session,authentication,owin,Asp.net,Session,Authentication,Owin,我对OWIN完全陌生,这个问题一直是我的主要障碍 基本上,在我的MVC应用程序中,我在启动课程中有以下内容: public partial class Startup { public void ConfigureAuth(IAppBuilder app) { app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType); app.
public partial class Startup
{
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = OfficeSettings.ClientId,
Authority = OfficeSettings.Authority,
TokenValidationParameters = new System.IdentityModel.Tokens.TokenValidationParameters()
{
RoleClaimType = "roles"
},
Notifications = new OpenIdConnectAuthenticationNotifications()
{
AuthorizationCodeReceived = (context) =>
{
// code hidden for readability
if(HttpContext.Current.Session == null)
{
// It's null. Why is that?
}
var session = HttpContext.Current.Session;
if (session["myMockSession"] != null)
{
// Do stuff...
}
},
RedirectToIdentityProvider = (context) =>
{
// code hidden for readability
},
AuthenticationFailed = (context) =>
{
// code hidden for readability
}
}
});
我不明白为什么调试时会话为空。HttpContext。当前属性不是。Sessions+OWIN是否有任何限制?这个问题有解决办法吗?应该如何处理
旁注1:
我尝试添加在SO问题中发现的这段代码,但会话仍然为空:
app.Use((context, next) =>
{
// Depending on the handler the request gets mapped to, session might not be enabled. Force it on.
HttpContextBase httpContext = context.Get<HttpContextBase>(typeof(HttpContextBase).FullName);
httpContext.SetSessionStateBehavior(SessionStateBehavior.Required);
return next();
});
app.Use((上下文,下一步)=>
{
//根据请求映射到的处理程序,会话可能未启用。请强制启用它。
HttpContextBase httpContext=context.Get(typeof(HttpContextBase.FullName);
httpContext.SetSessionStateBehavior(SessionStateBehavior.Required);
返回next();
});
旁注2:
我似乎再也找不到了,但有人在其中一个问题中建议在Global.asax中添加空方法Session_Start和Session_End(作为空方法)。那也没用
我欢迎任何建议。
谢谢 你差不多到了那里。 会话仍然为空的原因是您没有指示OWIN初始化System.Web会话,中间件之前的会话正在执行 通过在中间件注册之后添加.UseStageMarker(..),您将告诉OWIN它应该在执行管道的何处执行SetSessionStateBehavior Net katana文档对中间件的工作方式有详细的说明。
有关Asp.net中执行顺序的详细信息,请参阅枚举文档和文档 我也有类似的问题。你能找到解决办法吗?嗨,RonakThakkar。不幸的是,还没有。我不得不把这项任务搁置一边,因为我找不到任何解决办法。也许我们可以在这个问题上找到一个解决方案,希望如此。嗨,AuroMetal,谢谢你发布这个问题。我一直在努力让它工作,但我不确定在哪里插入@johano的解决方案。如果有人能提供更多的细节,我将不胜感激。嗨@AP,如果我没弄错你的意思,你会问我该把代码放在哪里?请记住,这个问题已经提出两年多了,但我相信,当您创建ASP.NET Web应用程序>MVC模板(更改身份验证-个人用户| | |工作或学校帐户)时,会创建一个App|u Start目录,在该目录下有一个Startup.Auth.cs,在那里可以插入解决方案。再说一次,时间太长了,我可能错了,所以提前道歉。嗨@Johan O,解释得太棒了!非常感谢您提供的详细信息,希望这能帮助我们中的一些人解决同样的问题。
app.Use((context, next) =>
{
var httpContext = context.Get<HttpContextBase>(typeof(HttpContextBase).FullName);
httpContext.SetSessionStateBehavior(SessionStateBehavior.Required);
return next();
});
// To make sure the above `Use` is in the correct position:
app.UseStageMarker(PipelineStage.MapHandler);
.Use((context, next) =>
{
// now use the session
HttpContext.Current.Session["test"] = 1;
return next();
})
.UseStageMarker(PipelineStage.PostAcquireState);