【发布时间】:2011-08-02 12:36:44
【问题描述】:
基本上我一直被我的 asp.net mvc 应用程序抛出,因为 User.Identity.IsAuthenticated 是错误的,这只是因为下面的代码与任务 cmets 相关(下面标记为 **) - 我不知道为什么它正在发生,任何帮助表示赞赏。
下面的代码在我的基本控制器上对用户进行身份验证的自定义属性中,如果未通过身份验证,我会抛出如下异常:
if (!httpContext.User.Identity.IsAuthenticated)
throw new NoAccessException("unauthorized user"); // invalid users are thrown out...
导致 User.Identity.IsAuthenticated + User.Identity.Name 变为 null 的代码是:
[HttpGet]
public ActionResult TaskDetail(int houseid, int taskid)
{
//NOTE: _repo is a simple ISession over Linq to Sql
//GetCurrentUser() is a extention method which gets the current logged on user
//i.e. User.Identity.Name so I can get the users credentials
var loggedonuser = _repo.GetCurrentUser();
var _house= _repo.Single<House>(x => x.HouseID== houseid&& x.ClientID== loggedonuser.CompanyID);
if (_house== null)
throw new NoAccessException();
var summary = _instruction.ToSummaryDTO();
var companies = _repo.All<Company>();
//var users = _repo.All<User>();
var task = _repo.Single<Task>
(x => x.HouseID== _house.HouseID && x.CompanyID == loggedonuser.CompanyID);
var dto = new TaskDTO
{
TaskID = task.TaskID,
Title = task.Title,
Description = task.Description,
DateCreated = task.DateCreated,
IsClosed = task.IsClosed,
CompanyID = companies.Where(y => task.CompanyID == y.CompanyID).SingleOrDefault().Identifier
};
**dto.AllComments** = _repo.All<TaskComment>()
.Where(x => x.TaskID == task.TaskID)
.OrderByDescending(x => x.Timestamp)
.Select(x => new TaskCommentDTO
{
Comment = x.Comment,
Timestamp = x.Timestamp,
CompanyID = companies.Where(y => x.CompanyID == y.CompanyID).SingleOrDefault().Identifier
});
return View(new TaskViewModel
{
Summary = summary,
TaskDetail = dto,
});
}
注意:如果我省略了 dto.AllComments (IQueryable),那么一切正常,我永远不会被抛出我的系统或更重要的是 User.Identitiy 仍然是正确的......我试图转换为列表 - 这是我理想中想要的,但它也不起作用,也许我的 linq 方法有问题......
我的 DTO:
public class TaskDTO
{
public int TaskID { get; set; }
public bool IsClosed { get; set; }
public string CompanyID { get; set; }
public string AssignedTo { get; set; }
public DateTime DateCreated { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public IQueryable<TaskCommentDTO> AllComments { get; set; }
}
public class TaskCommentDTO
{
public string CompanyID { get; set; }
public string UserID { get; set; }
public DateTime Timestamp { get; set; }
public string Comment { get; set; }
}
编辑:抛出异常的地方
我现在已经跟踪了异常,我在我的基本控制器中覆盖了下面的代码,这有助于我发现错误:
protected override void Execute(System.Web.Routing.RequestContext requestContext)
{
base.Execute(requestContext);
}
页面完成加载后,再次运行此代码,当我在 RouteData 的值中检查请求上下文时,我发现“错误”和“FileNotFound”,此时用户也被清空,现在我需要找出哪个文件没有找到... :(
【问题讨论】:
-
您是否尝试调试以查看究竟是哪一行引发了异常?尝试单步执行并观察变量,看看会发生什么。
-
是 - 一旦我的过滤器运行,NoAccessException 就会如上所示抛出,在此之前我无法跟踪,因为我不确定在我的过滤器之前运行什么代码......除了我提到如果我删除与 cmets 相关的代码一切正常...
-
你觉得 linq 查询没问题吗?
-
在下面发布以尝试帮助跟踪问题所在。
标签: c# linq linq-to-sql asp.net-mvc-2