对于路由请求时HttpContext.Current.Session为null感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍路由器的http,并为您提供关于.netHttpContext.Cu
对于路由请求时HttpContext.Current.Session为null感兴趣的读者,本文将会是一篇不错的选择,我们将详细介绍路由器的http,并为您提供关于.net HttpContext.Current.Request.RawUrl、.net – 为什么HttpContext.Current.Handler为null?、ajax – 设置HttpContext.Current.User、Asp.net System.Web.HttpContext.Current.Session null in global.asax的有用信息。
本文目录一览:- 路由请求时HttpContext.Current.Session为null(路由器的http)
- .net HttpContext.Current.Request.RawUrl
- .net – 为什么HttpContext.Current.Handler为null?
- ajax – 设置HttpContext.Current.User
- Asp.net System.Web.HttpContext.Current.Session null in global.asax
路由请求时HttpContext.Current.Session为null(路由器的http)
没有路由,HttpContext.Current.Session
有没有,所以我知道它StateServer
正在工作。当我的路线我的要求,HttpContext.Current.Session
是null
在路由页面。我在IIS
7.0上使用.NET 3.5
sp1,但没有MVC预览。似乎AcquireRequestState
在使用路由时永远不会触发,因此不会实例化/填充会话变量。
当我尝试访问Session变量时,出现以下错误:
base {System.Runtime.InteropServices.ExternalException} = {"Session state canonly be used when enableSessionState is set to true, either in a configurationfile or in the Page directive. Please also make sure thatSystem.Web.SessionStateModule or a custom session state module is included inthe <configuration>.
在进行调试时,我还收到了HttpContext.Current.Session
在该上下文中不可访问的错误。
-
我的web.config
样子是这样的:
<configuration> ... <system.web> <pages enableSessionState="true"> <controls> ... </controls> </pages> ... </system.web> <sessionState cookieless="AutoDetect" mode="StateServer" timeout="22" /> ...</configuration>
这是IRouteHandler的实现:
public class WebPageRouteHandler : IRouteHandler, IRequiresSessionState{ public string m_VirtualPath { get; private set; } public bool m_CheckPhysicalUrlAccess { get; set; } public WebPageRouteHandler(string virtualPath) : this(virtualPath, false) { } public WebPageRouteHandler(string virtualPath, bool checkPhysicalUrlAccess) { m_VirtualPath = virtualPath; m_CheckPhysicalUrlAccess = checkPhysicalUrlAccess; } public IHttpHandler GetHttpHandler(RequestContext requestContext) { if (m_CheckPhysicalUrlAccess && !UrlAuthorizationModule.CheckUrlAccessForPrincipal( m_VirtualPath, requestContext.HttpContext.User, requestContext.HttpContext.Request.HttpMethod)) { throw new SecurityException(); } string var = String.Empty; foreach (var value in requestContext.RouteData.Values) { requestContext.HttpContext.Items[value.Key] = value.Value; } Page page = BuildManager.CreateInstanceFromVirtualPath( m_VirtualPath, typeof(Page)) as Page;// IHttpHandler; if (page != null) { return page; } return page; }}
我也尝试过将EnableSessionState="True"
aspx页面放在顶部,但是什么也没做。
有什么见解吗?我应该编写另一个HttpRequestHandler
实现的工具IRequiresSessionState
吗?
谢谢。
答案1
小编典典得到它了。实际上,这很愚蠢。在我删除并添加SessionStateModule后,它的工作原理如下:
<configuration> ... <system.webServer> ... <modules> <remove name="Session" /> <add name="Session" type="System.Web.SessionState.SessionStateModule"/> ... </modules> </system.webServer></configuration>
仅仅添加它是行不通的,因为“会话”应该已经在中定义了machine.config
。
现在,我想知道这是否是平常的事情。由于它看起来如此粗略,因此肯定不会如此……
.net HttpContext.Current.Request.RawUrl
.net HttpContext.Current.Request.RawUrl 获取当前请求的原始URL 是什么意思什么是原始URL,取得的值是什么,在JAVA中该如何实现 请指教
.net – 为什么HttpContext.Current.Handler为null?
我正在使用.Net 3.5框架开发.
我在AuthorizeRequest和AuthenticateRequest上检查这个
谢谢.
解决方法
ajax – 设置HttpContext.Current.User
FormsAuthentication.SetAuthCookie(person.LoginName,false);
是不足以填充’HttpContext.Current.User’,它需要运行以下方法:
FormsAuthentication.RedirectFromLoginPage("...");
问题在于,正如我所提到的,loggin表单使用ajax表单,并使用json获取响应,因此无法重定向.
我怎么能填写’HttpContext.Current.User’?
谢谢.
更新:
这是注册方法:
[HttpPost] public ActionResult Register(Person person) { var q = da.Persons.Where(x => x.LoginName == person.LoginName.ToLower()).FirstOrDefault(); if (q != null) { ModelState.AddModelError("","Username is repettive,try other one"); return Json(new object[] { false,this.RenderPartialViewToString("RegisterControl",person) }); } else { if (person.LoginName.ToLower() == "admin") { person.IsAdmin = true; person.IsActive = true; } da.Persons.Add(person); da.SaveChanges(); FormsAuthentication.SetAuthCookie(person.LoginName,false); return Json(new object[] { true,"You have registered successfully!" }); } }
>如果已经过身份验证,请不要做任何事情
>不修改cookie,因为这应仅用于此请求的生命周期
>缩短一些东西,用userdata更安全一些(永远不应该为null,但……)
在调用SetAuthCookie()后调用此方法,如下所示:
// in login function FormsAuthentication.SetAuthCookie(model.UserName,model.RememberMe); AuthenticateThisRequest(); private void AuthenticateThisRequest() { //NOTE: if the user is already logged in (e.g. under a different user account) // then this will NOT reset the identity information. Be aware of this if // you allow already-logged in users to "re-login" as different accounts // without first logging out. if (HttpContext.User.Identity.IsAuthenticated) return; var name = FormsAuthentication.FormsCookieName; var cookie = Response.Cookies[name]; if (cookie != null) { var ticket = FormsAuthentication.Decrypt(cookie.Value); if (ticket != null && !ticket.Expired) { string[] roles = (ticket.UserData as string ?? "").Split(','); HttpContext.User = new GenericPrincipal(new FormsIdentity(ticket),roles); } } }
编辑:删除对Request.Cookies的调用,如@ AdamTuplier-MSFT所述.
Asp.net System.Web.HttpContext.Current.Session null in global.asax
但是,我只是添加一个动态图像功能,让一个页面提供图像,每当加载动态图像页面System.Web.HttpContext.Current.Session是null在global.asax,这阻止我设置安全校长是从那时开始的正常和级联问题.
通常,在用户登录时,在开始时的会话期间,global.asax中的Session仅为null,之后它始终可用于此单个异常.
当浏览器碰到原始页面中的图像时,加载动态图像页面,即
我猜这是浏览器在不发送一些凭据的情况下请求该页面的一个方面呢?
任何帮助将不胜感激.
解决方法
我假设你正在为处理程序使用一个ashx处理程序.如果是这样,请务必从IRequiresSessionState派生例如:
public class Images : IHttpHandler,System.Web.SessionState.IRequiresSessionState { }
如果您不使用ashx,您可以描述动态图像页面的意思吗?
玩笑
今天的关于路由请求时HttpContext.Current.Session为null和路由器的http的分享已经结束,谢谢您的关注,如果想了解更多关于.net HttpContext.Current.Request.RawUrl、.net – 为什么HttpContext.Current.Handler为null?、ajax – 设置HttpContext.Current.User、Asp.net System.Web.HttpContext.Current.Session null in global.asax的相关知识,请在本站进行查询。
本文标签: