ASP.Net MVC 3奇怪的会话行为
发布时间:2020-12-16 03:20:47 所属栏目:asp.Net 来源:网络整理
导读:我有一个mvc 3应用程序,我正在使用我自己的登录视图实现授权,该视图检查是否允许用户名和密码,然后在会话中设置一个变量来表示用户已登录.这种方法有效但是对于一个特定的观点,它表现出一种奇怪的不良方式.所述视图包含一个表单,我用它来输入一些数据并上传
|
我有一个mvc 3应用程序,我正在使用我自己的登录视图实现授权,该视图检查是否允许用户名和密码,然后在会话中设置一个变量来表示用户已登录.这种方法有效但是对于一个特定的观点,它表现出一种奇怪的不良方式.所述视图包含一个表单,我用它来输入一些数据并上传文件.由于某些我无法弄清楚的原因,在发布此表单后,将启动一个新会话,因此记住用户已登录的变量将重置为false,然后再次显示登录页面.
我迷失了为什么应用程序此时正在开始一个新的会话?我没有指示它这样做.任何人都可以推荐解决方案来阻止这种行为并让它保持旧会话? 谢谢. 更新 – 一些代码: 请注意,会话似乎在对发布的“创建”表单的响应后立即终止 CMS控制器在所有操作上使用名为“RDAutorize”的自定义Autorize属性: [RDAuthorize]
public class PhotoCMSController : Controller
{
public ActionResult Create()
{
/* Code omitted: set up a newPhoto object with default state */
/* Display view containing form to upload photo and set title etc. */
return View("../Views/PhotoCMS/Create",newPhoto);
}
[HttpPost]
public ContentResult Upload(int pPhotoId)
{
/* Code ommited: receive and store image file which was posted
via an iframe on the Create view */
string thumbnail = "<img src='/path/to/thumb.jpg' />";
return Content(thumbnail);
}
[HttpPost]
public ActionResult Create(string pPhotoTitle,string pCaption etc...)
{
/*Code omitted: receive the rest of the photo data and save
it along with a reference to the image file which was uploaded
previously via the Upload action above.*/
/* Display view showing list of all photo records created */
return View("../Views/PhotoCMS/Index",qAllPhotos.ToList<Photo>());
/* **Note: after this view is returned the Session_End() method fires in
the Global.asax.cs file i.e. this seems to be where the session is
being lost** */
}
}/*End of CMS Controller*/
自定义授权操作过滤器: public class RDAuthorize : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
Boolean authorized = Convert.ToBoolean(
HttpContext.Current.Session["UserIsAuthorized"]
);
if (!authorized) {
/* Not logged in so send user to the login page */
filterContext.HttpContext.Response.Redirect("/Login/Login");
}
}
public override void OnActionExecuted(ActionExecutedContext filterContext) {}
public override void OnResultExecuting(ResultExecutingContext filterContext) {}
public override void OnResultExecuted(ResultExecutedContext filterContext) {}
}/*End of Authorize Action Filter*/
登录控制器: public class LoginController : Controller
{
private PhotoDBContext _db = new PhotoDBContext();
public ActionResult Login()
{
string viewName = "";
Boolean authorized = Convert.ToBoolean(Session["UserIsAuthorized"]);
if (authorized)
{
viewName = "../Views/Index";
}
else
{
viewName = "../Views/Login/Login";
}
return View(viewName);
}
[HttpPost]
public ActionResult Login(string pUsername,string pPassword)
{
string viewName = "";
List<Photo> model = new List<Photo>();
var qUsers = from u in _db.Users
select u;
foreach (User user in qUsers.ToList<User>())
{
/* If authorized goto CMS pages */
if (pUsername == user.Username && pPassword == user.Password)
{
Session["UserIsAuthorized"] = true;
var qPhotos = from p in _db.Photos
where p.IsNew == false
select p;
model = qPhotos.ToList<Photo>();
viewName = "../Views/PhotoCMS/Index";
break;
}
}
return View(viewName,model);
}
}/* End of Login controller */
解决方法
事实证明整个ASP.Net应用程序正在重新启动,因为作为照片上传的一部分,我将图像文件存储在临时文件夹中,然后在将文件移动到永久位置后删除目录.显然,如果删除了网站中的目录,ASP.Net的默认行为将重新启动.我发现这个
post
它描述了问题并提供了一个解决方案,将以下代码添加到Global.asax.cs文件中.实施此解决方案已解决了该问题.通过从Application_Start()事件调用FixAppDomainRestartWhenTouchingFiles()来应用此修复: protected void Application_Start()
{
FixAppDomainRestartWhenTouchingFiles();
}
private void FixAppDomainRestartWhenTouchingFiles()
{
if (GetCurrentTrustLevel() == AspNetHostingPermissionLevel.Unrestricted)
{
/*
From: http://www.aaronblake.co.uk/blog/2009/09/28/bug-fix-application-restarts-on-directory-delete-in-asp-net/
FIX disable AppDomain restart when deleting subdirectory
This code will turn off monitoring from the root website directory.
Monitoring of Bin,App_Themes and other folders will still be
operational,so updated DLLs will still auto deploy.
*/
PropertyInfo p = typeof(HttpRuntime).GetProperty(
"FileChangesMonitor",BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
object o = p.GetValue(null,null);
FieldInfo f = o.GetType().GetField(
"_dirMonSubdirs",BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase);
object monitor = f.GetValue(o);
MethodInfo m = monitor.GetType().GetMethod(
"StopMonitoring",BindingFlags.Instance | BindingFlags.NonPublic);
m.Invoke(monitor,new object[] { });
}
}
private AspNetHostingPermissionLevel GetCurrentTrustLevel()
{
foreach (AspNetHostingPermissionLevel trustLevel in
new AspNetHostingPermissionLevel[] {
AspNetHostingPermissionLevel.Unrestricted,AspNetHostingPermissionLevel.High,AspNetHostingPermissionLevel.Medium,AspNetHostingPermissionLevel.Low,AspNetHostingPermissionLevel.Minimal }
)
{
try
{
new AspNetHostingPermission(trustLevel).Demand();
}
catch (System.Security.SecurityException)
{
continue;
}
return trustLevel;
}
return AspNetHostingPermissionLevel.None;
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-core – 在VS 2015 RC中,编译保存不适用于ASP.NET
- asp.net-mvc – 使用Linq返回空字符串到SQL – 没有行的相关
- asp.net-mvc – 我可以在asp.net mvc中结束视图的渲染吗?
- asp.net-mvc – 如何在EditorTemplate中获取完全限定的成员
- iis – 经典ASP – 请求对象为空
- asp.net – 在PostBack期间Recaptcha消失
- 在ASP.NET中访问Active Directory?
- asp.net – 在jQuery对话框中的窗体中,MVC中通常采用的方法
- 使用.NET 4.5的ASP.Net成员身份和实体框架迁移的问题
- ASP.Net Checkbox值在回发错误?
推荐文章
站长推荐
- asp.net-mvc – mvc创建我自己的html帮助器,如何
- ASP.Net MVC JQuery在IE8中未定义,但在Chrome中没
- asp.net-mvc – 错误NU1002项目WebApplication1中
- asp.net – 控制Web / MSDeploy发布的内容:PDF文
- 如何删除IIS自定义标头像X-Powered-By:ASP.NET从
- 与asp.net mvc的dotnetopenauth证明太沮丧使用
- asp.net-mvc-3 – 如何从ModelMetadata检索Group
- ASP.NET MVC3对模型的更改
- asp.net – aspx页面中的代码是否在Web应用程序中
- asp.net-web-api – 与web api和web api 2之间的
热点阅读
