简体   繁体   English

获取当前用户ASP.NET成员身份的ID

[英]Get the ID of the current user ASP.NET Membership

How do you guys manage to get the ID of the current user using the ASP.NET Membership Provider? 你们如何设法使用ASP.NET成员资格提供程序获取当前用户的ID? My application is an MVC 3 using Database First approach, so I created my own database and the Membership Provider and Role Provider are using my tables (Custom Membership and Role Provider). 我的应用程序是使用Database First方法的MVC 3,因此我创建了自己的数据库,成员资格提供程序和角色提供程序正在使用我的表(自定义成员资格和角色提供程序)。

In one of my page (feedback), user can send feedback about my website and if he's connected, I want to include the UserID in the table when I insert. 在我的一个页面(反馈)中,用户可以发送有关我的网站的反馈,如果他已连接,我想在插入时将UserID包含在表格中。

But since the ASP.NET Membership is an IIdentity, the only thing I can access is the Name. 但由于ASP.NET Membership是一个IIdentity,我唯一可以访问的是Name。 I don't want to create a link between 2 tables using the name, I want the ID! 我不想使用名称在2个表之间创建链接,我想要ID!

So what do you do in your application for this VERY simple task? 那么你在申请这项非常简单的任务时做了什么? I mean, in almost every page where a connected user needs to insert a value, we need the ID of the user to create the correct foreign key to the user table, right? 我的意思是,在连接用户需要插入值的几乎每个页面中,我们都需要用户的ID来为用户表创建正确的外键,对吧? By the way, I'm very new to this MVC framework, before that I was using ASP.NET aspx and for the user information, I was using the Session which gave me the current ID, Name and other fields as needed. 顺便说一下,我对这个MVC框架很新,在我使用ASP.NET aspx和用户信息之前,我正在使用Session,它根据需要给了我当前的ID,Name和其他字段。 Since this implementation using the Session object gave me trouble in shared hosting, I want to use the real membership. 由于使用Session对象的这个实现给了我共享托管的麻烦,我想使用真正的成员资格。

Thanks! 谢谢!

EDIT 编辑

The ID I want must be an integer (in fact, I want the UserID field that is in the User table). 我想要的ID必须是一个整数(事实上,我想要User表中的UserID字段)。

Try this; 试试这个;

MembershipUser mu = Membership.GetUser("username");
string userId = mu.ProviderUserKey.ToString();

For currently logged in user you could use without passing the username; 对于当前登录的用户,您可以使用而不传递用户名;

string userId = Membership.GetUser().ProviderUserKey.ToString();

我想分享如何获取Identity 2.0的UserId:

string UserId = System.Web.HttpContext.Current.User.Identity.GetUserId();

I ran into a similar problem. 我遇到了类似的问题。 I (for other reasons, as well) made a base Controller class that all of my other controllers inherited from. 我(由于其他原因,也)创建了一个基本的Controller类,我的所有其他控制器都继承自。 You can have the base class hold the user's information for use at any time. 您可以让基类随时保存用户的信息。

assuming you have everything configured correctly for your providor in your web.config, and since you can get the username, I recommend: 假设您在web.config中为您的提供者正确配置了所有内容,并且由于您可以获得用户名,我建议:

Dim muser As MembershipUser = Membership.GetUser(userName)

Here are details on the MembershipUser object: http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k%28SYSTEM.WEB.SECURITY.MEMBERSHIPUSER%29;k%28TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV4.0%22%29;k%28DevLang-VB%29&rd=true 以下是MembershipUser对象的详细信息: http//msdn.microsoft.com/query/dev10.query?appId = Dev10IDEF1&l = EN-US&k = k%28SYSTEM.WEB.SECURITY.MEMBERSHIPUSER%29; k%28TargetFrameworkMoniker-%22 .NETFRAMEWORK%2cVERSION%3dV4.0%22%29; K-%28DevLang-VB%29&RD =真

many properties other than name available. 除了名称以外的许多属性。 The ID feild you want is called 'ProviderUserKey' 您想要的ID字段称为“ProviderUserKey”

Could this be what your after? 这可能是你的追求吗?

userid from asp.net Membership Authentication? 来自asp.net会员身份验证的userid?

Personally I have my own class which encapsulates retrieving these details by the username plus any custom data I require in my application. 就个人而言,我有自己的类,它封装了通过用户名检索这些细节以及我在我的应用程序中需要的任何自定义数据。

Regards 问候

要获取当前用户ID,您可以尝试以下操作

string currentUserId= Convert.ToString(Membership.GetUser().ProviderUserKey);

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

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