您如何使用User.Identity.GetUserId(); [英] How do you use User.Identity.GetUserId();

查看:308
本文介绍了您如何使用User.Identity.GetUserId();的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio,并且是我的新手.我正在尝试执行以下操作:

  • 用户登录并单击一个名为主题"的页面
  • 检查用户已登录(如果未登录,则显示您必须登录.")
  • 如果用户已登录,请从主题"表中获取其信息(我应该注意,我的表的结构不是正确"的,因为此时我只是在测试/学习一些知识).
  • 如果在教师"下的科目表"中,该值为0-返回页面,显示消息您选择的科目列表",否则,返回您教的科目列表".

我已经为此工作了几个小时,并且不断收到错误消息.主要是我认为我不了解User.Identity.GetUserId();之所以有效,是因为我试图将它返回的值与subjects表中的userID进行比较,因为我假设它是从AspNetUsers表中获取ID的,所以我尝试匹配它们.当我尝试访问主题页面时出现错误:其他信息:DbComparisonExpression需要具有可比较类型的参数."它们都是相同的类型(nvarchar),在此之前它们是不同的数据类型,我一直在比较中遇到问题,这就是为什么我更改它们进行测试的原因.

所以无论如何,我认为问题是我不知道User.Identity.GetUserId();工作,它究竟返回什么.我正在做一个假设,即它从AspNetUsers表返回UserId,不知道为什么,但可能是为什么我会得到比较错误.

这是我的代码的一部分:

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

我只是想学习如何获取登录用户的信息,以从我制作的主题"表中获取他们的信息.

解决方案

我不知道User.Identity.GetUserId();有效,到底是什么 返回

它从您的身份用户表(在您的情况下为dbo.AspNetUsers表)中返回字符串形式的登录用户的用户ID.

请确保您在dbo.Students表中为StudentId使用相同的数据类型(字符串),然后从SQL Server的表设计"中再次确认.

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.

解决方案

I don't know how 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.

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.

这篇关于您如何使用User.Identity.GetUserId();的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆