User.Identity.Name returns guid
本文关键字:guid returns Name Identity User | 更新日期: 2023-09-27 18:04:48
由于某些(模糊)原因,我的MVC 4应用程序在运行时返回一个guid:
var name = User.Identity.Name;
我也在一个全新的MVC 4应用程序中测试了这一点,这对我来说是新的行为。当我查找关于IIdentity的文档时,它指出(我记得)Identity。名称应该
获取当前用户的名称。
为什么在我的情况下返回一个guid ?
来自web.config
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
更多相关信息:应用程序还部署在另一台机器上,与同一个数据库通信。我们使用"User.Identity"的值。Name",当数据库中没有用户(新用户)的id时,我们用该id添加一个新用户。
现在,奇怪的是:当您切换应用程序(从本地主机切换到部署在T上的应用程序)时,您需要再次登录。然后将User.Identity.Name设置为一个新向导。
(对于starter默认应用程序,我们当然不会与DB对话,但同样的事情会发生;User.Identity.Name返回guid)
您需要找到与当前用户对应的通用主体对象。
using System.Security.Principal;
...
GenericPrincipal genericPrincipal = GetGenericPrincipal();
GenericIdentity principalIdentity = (GenericIdentity)genericPrincipal.Identity;
string fullName = principalIdentity.Name;