User.Identity.Name变为null但我不明白为什么

时间:2011-08-02 12:36:44

标签: c# linq linq-to-sql asp.net-mvc-2

基本上我一直被我的asp.net mvc应用程序抛出,因为User.Identity.IsAuthenticated是假的,这只是因为下面的代码与任务注释相关(下面标记为**) - 我无法弄清楚为什么它正在发生,任何帮助都表示赞赏。

我的基本控制器上的自定义属性中的代码,用于对用户进行身份验证,如果未经过身份验证,我会抛出异常,如下所示:

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的值中找到“Error”和“FileNotFound”,此时用户也被淘汰了,现在我需要找出哪个文件找不到... :(

1 个答案:

答案 0 :(得分:0)

从你的代码:

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
    };

这一行:

CompanyID = companies.Where(y => task.CompanyID == y.CompanyID).SingleOrDefault().Identifier

Identifier结果上调用属性SingleOrDefault(),该结果可能是null。你有没有检查过这个没问题。是否在这一行抛出了异常?

注意,您可以使用IEnumerable代替IQueryable;可能不会有任何区别,但如果这是崩溃的部分,你可以毫无意义地尝试。