Tom*_*Tom 7 .net c# asp.net-mvc asp.net-mvc-4
对于某些(模糊)原因,我的MVC 4应用程序在运行时返回一个guid:
var name = User.Identity.Name;
Run Code Online (Sandbox Code Playgroud)
我也在一个全新的MVC 4应用程序中对此进行了测试,这对我来说是新的行为.当我查阅有关IIdentity的文档时,它表明(我记得)Identity.Name应该
获取当前用户的名称.
为什么它会在我的情况下返回一个guid?
来自web.config
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
Run Code Online (Sandbox Code Playgroud)
更多相关信息: 该应用程序也部署在另一台计算机上,与同一数据库通信.我们在该数据库中使用"User.Identity.Name"的值,当用户(新用户)不在那里时,我们添加一个具有该guid的新用户.
现在,奇怪的是:当您切换应用程序时(从localhost到部署在T上的应用程序),您需要再次登录.然后将User.Identity.Name设置为新的guid.
(使用启动器默认应用程序,我们当然不会与DB通信,但同样的事情发生; User.Identity.Name返回一个guid)
您需要找到与当前用户相对应的通用主体对象。
using System.Security.Principal;
...
GenericPrincipal genericPrincipal = GetGenericPrincipal();
GenericIdentity principalIdentity = (GenericIdentity)genericPrincipal.Identity;
string fullName = principalIdentity.Name;
Run Code Online (Sandbox Code Playgroud)