I am using Visual Studio and I'm new to it. I am trying to do the following:
- The user logs in and clicks a page called "subjects"
- Check the user is logged in (if not, display "you must be logged in..")
- If the user is logged in, obtain their information from the Subjects table (I should note my tables are not structured "properly", as at this time I am just testing/learning a few things).
- If in the Subjects table under "tutor" the value is 0 - return page with message "List of the subjects you take", otherwise, return "List of the subjects you teach".
I've been working on this for hours and I keep getting errors. Mainly I think I don't understand how User.Identity.GetUserId(); works, because I am trying to compare the value it returns to the userID in the subjects table, because I assumed it gets the ID from the AspNetUsers table, so I tried to match them. I am getting an error when I try to access the subjects page: "Additional information: DbComparisonExpression requires arguments with comparable types." They are both the same type (nvarchar) and before this they were different data types and I kept having problems with comparisons which is why I changed them, to test it.
So anyway I think the problem is I don't know how User.Identity.GetUserId(); works, what exactly it returns. I was working on the assumption it returns the UserId from AspNetUsers table, not sure why, but probably why I get comparison errors.
This is part of my code:
SubjectsContext1 db = new SubjectsContext1(); if (User.Identity.IsAuthenticated) { //check if user is logged in var userInfo = User.Identity.GetUserId(); //get logged in user info //find logged in user info from Subjects table (errors here) var userData = (from c in db.Subjects where c.studentID.Equals(userInfo) select c).FirstOrDefault(); var email = userData.email; var tutorStatus = userData.tutor; if(tutorStatus == 0) { ViewBag.Message = "A list of the subjects you take."; return View(); } else { ViewBag.Message = "A list of the subjects you teach."; return View(); } } else { ViewBag.Message = "You must be logged in to view this page!"; return View(); }
I just want to learn how I can get the logged in user's information to get their information from the Subjects table I made.