如何在MVC应用程序中获取当前用户? [英] How do I get the current user in an MVC Application?

查看:133
本文介绍了如何在MVC应用程序中获取当前用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何获取Asp.Net MVC中的当前用户

我已经尝试了一些不同的东西,我知道我以前做过,当我使用Webforms,但我似乎无法弄清楚如何在MVC中。

I've tried a bunch of different things, I know I've done it before when I used Webforms, but I can't seem to figure out how to do it in MVC.

会员资格不起作用
用户给了我一个IPrincipal或一些User.Identity给了我相同类型的变量...

Membership doesn't work User gives me an IPrincipal or something User.Identity gives me the same type of variable...

任何人都知道?有没有GetCurrentUser()方法或类似的东西?

Anyone know? Is there any GetCurrentUser() method or something similar?

推荐答案

请参阅Mehrdad的答案:System.Web.UI.Page不会让我从ASP.Net-MVC中的Controller访问CurrentUser或User.Identity

See Mehrdad's answer here: System.Web.UI.Page won't let me access CurrentUser or User.Identity from a Controller in ASP.Net-MVC

他解释说可以使用

User.Identity // in the controller
HttpContext.User.Identity // in the controller
System.Web.HttpContext.Current.User.Identity // anywhere

这篇关于如何在MVC应用程序中获取当前用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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