简体   繁体   English

您如何使用User.Identity.GetUserId();

[英]How do you use User.Identity.GetUserId();

I am using Visual Studio and I'm new to it. 我正在使用Visual Studio,并且是我的新手。 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". 如果在“教师”下的“主题”表中,该值为0-返回页面,显示消息“您选择的学科列表”,否则返回“您教的主题列表”。

I've been working on this for hours and I keep getting errors. 我已经为此工作了几个小时,并且不断收到错误消息。 Mainly I think I don't understand how User.Identity.GetUserId(); 主要是我认为我不了解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. 之所以可行,是因为我试图将它返回的值与subjects表中的userID进行比较,因为我假设它是从AspNetUsers表中获取ID的,所以我尝试匹配它们。 I am getting an error when I try to access the subjects page: "Additional information: DbComparisonExpression requires arguments with comparable types." 尝试访问主题页面时出现错误:“其他信息:DbComparisonExpression需要具有可比较类型的参数。” 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. 它们都是相同的类型(nvarchar),在此之前它们是不同的数据类型,我在比较时一直遇到问题,这就是为什么我更改它们进行测试的原因。

So anyway I think the problem is I don't know how User.Identity.GetUserId(); 所以无论如何,我认为问题是我不知道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. 我正在做一个假设,即它从AspNetUsers表返回UserId,不知道为什么,但是可能为什么会出现比较错误。

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. 我只想了解如何获取登录用户的信息,以从我制作的“主题”表中获取他们的信息。

I don't know how User.Identity.GetUserId(); 我不知道User.Identity.GetUserId(); works, what exactly it returns 有效,它到底返回了什么

It returns the user id of logged in user from your identity's user table (dbo.AspNetUsers table in your case) which is string. 它从身份的用户表(在您的情况下为dbo.AspNetUsers表)中返回字符串形式的登录用户的用户ID。

Make sure you are using the same data type (string) for StudentId in your dbo.Students table, reconfirm that from your "Table Design" in SQL Server. 确保在dbo.Students表中为StudentId使用相同的数据类型(字符串),然后从SQL Server的“表设计”中再次确认。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM