asp.net-mvc asp.net mvc 获取当前登录的用户 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2530896/
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
asp.net mvc get current logged in user id
提问by niao
Possible Duplicate:
How do you get the UserID of a User object in ASP.Net MVC?
Greetings, I have created a custom membership provider to make it possible to authenticate users on my application. How can i retrieve current user id in controller? I need this to get some data from database for this user like this:
您好,我创建了一个自定义成员资格提供程序,以便在我的应用程序上对用户进行身份验证。如何在控制器中检索当前用户 ID?我需要这个来从数据库中为这个用户获取一些数据,如下所示:
PersistanceManger.RepositoryUser.GetInformation(userROWGUID);
回答by Mahesh Velaga
Do you particularly need the Id of the user or the userName ?, it seems you are looking for the Guid of the present user, for this I think the retrieval of this Id depends on your implementation. If you want to retrieve information based of userName, then you can do the following:
您是否特别需要用户的 Id 或 userName ?,您似乎正在寻找当前用户的 Guid,为此我认为此 Id 的检索取决于您的实现。如果要根据用户名检索信息,则可以执行以下操作:
HttpContext.Current.Usergives you the User object, you can get the other information from it, for your case, you need to use HttpContext.Current.User.Identity.Name
HttpContext.Current.User为您提供 User 对象,您可以从中获取其他信息,对于您的情况,您需要使用 HttpContext.Current.User.Identity.Name

