获取Forms身份验证中的用户名

Luc*_*oli 33 c# asp.net forms-authentication windows-authentication

我正在使用Forms身份验证.

在Windows身份验证中获取我使用的PC的用户名: User.Identity.Name

我也需要在Forms身份验证中使用此信息,但User.Identity.Name不起作用.

如何在不使用Windows身份验证的情况下获取User.Identity.Name?

Mat*_*att 59

要获取经过身份验证的用户的UserName:

HttpContext.Current.User.Identity.Name;
Run Code Online (Sandbox Code Playgroud)

  • 它返回一个空字符串. (2认同)
  • 您确定在web.config文件中正确设置了表单身份验证吗?这回复'this.Context.User.Identity.IsAuthenticated' (2认同)
  • @Luca:空字符串(通常)表示您是未经身份验证的实体. (2认同)