C# 获取当前用户的 ID ASP.NET Membership
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13159087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the ID of the current user ASP.NET Membership
提问by Jean-Fran?ois C?té
How do you guys manage to get the ID of the current user using the ASP.NET Membership Provider? 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).
你们如何使用 ASP.NET Membership Provider 获得当前用户的 ID?我的应用程序是一个使用数据库优先方法的 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. I don't want to create a link between 2 tables using the name, I want the ID!
但是由于 ASP.NET Membership 是一个 IIdentity,所以我唯一可以访问的是 Name。我不想使用名称在 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? 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. Since this implementation using the Session object gave me trouble in shared hosting, I want to use the real membership.
那么你在你的应用程序中为这个非常简单的任务做了什么?我的意思是,在连接用户需要插入值的几乎每个页面中,我们都需要用户的 ID 来为用户表创建正确的外键,对吗?顺便说一下,我对这个 MVC 框架很陌生,在此之前我使用的是 ASP.NET aspx,对于用户信息,我使用的是 Session,它根据需要为我提供了当前的 ID、名称和其他字段。由于这个使用 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 字段)。
采纳答案by Kaf
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();
回答by Corey Adler
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. You can have the base class hold the user's information for use at any time.
我遇到了类似的问题。我(也出于其他原因)创建了一个基本 Controller 类,我的所有其他控制器都继承自该类。您可以让基类保存用户的信息以供随时使用。
回答by Frank Thomas
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=true
many properties other than name available. The ID feild you want is called 'ProviderUserKey'
除了名称之外的许多属性可用。您想要的 ID 字段称为“ProviderUserKey”
回答by Beats
Could this be what your after?
这可能是你的追求吗?
userid from asp.net Membership Authentication?
Personally I have my own class which encapsulates retrieving these details by the username plus any custom data I require in my application.
就我个人而言,我有自己的类,它封装了通过用户名以及我在应用程序中需要的任何自定义数据来检索这些详细信息。
Regards
问候
回答by juhi
To get current UserID you can try following
要获取当前用户 ID,您可以尝试以下操作
string currentUserId= Convert.ToString(Membership.GetUser().ProviderUserKey);
回答by JoshYates1980
I wanted to share how to get the UserId for Identity 2.0:
我想分享如何获取 Identity 2.0 的 UserId:
string UserId = System.Web.HttpContext.Current.User.Identity.GetUserId();

