Basically I keep getting thrown out from my asp.net mvc application because the User.Identity.IsAuthenticated is false, this only happens because of the code below related to task comments (marked ** below) - I cannot figure out why it is happening, any help is appreciated.
Code below inside a custom attribute on my base controller that authenticates users, if not authenticated I throw an exception like so:
if (!httpContext.User.Identity.IsAuthenticated)
throw new NoAccessException("unauthorized user"); // invalid users are thrown out...
Code that is causing the User.Identity.IsAuthenticated + User.Identity.Name to become null is:
[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,
});
}
NOTE: If I omit the dto.AllComments (IQueryable) then everything works fine, I never get thrown out my system or more importantly User.Identitiy remains correct.... I have tried to convert to list - which is what I ideally want however that does not work either, maybe there is something wrong with my linq method...
My 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; }
}
EDIT: where exception is being thrown
I have traced the exception now, I overrided the code below in my base controller which helped me discover the bug:
protected override void Execute(System.Web.Routing.RequestContext requestContext)
{
base.Execute(requestContext);
}
After the page finished loading, this code was ran once again, when I checked out the request context In the Values for RouteData I found "Error" and "FileNotFound", at this point the user gets nulled out too, now I need to find out which file is not found... :(
From your code:
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
};
This line:
CompanyID = companies.Where(y => task.CompanyID == y.CompanyID).SingleOrDefault().Identifier
calls the property Identifier
on a SingleOrDefault()
result which could be null
. Have you checked that this is ok. Is it on this line that the exception is thrown?
Note you can use IEnumerable
instead of IQueryable
; probably won't make any difference but if that's the crashing part you can try it for nothing.
精彩评论