asp.net-mvc - 我需要在 MVC 5 中获取当前用户名
问题描述
我需要使用 HttpContext.Current.User.Identity.Name,但它总是返回为空。我在项目属性中将匿名身份验证设置为禁用,并将 Windows 身份验证设置为启用。我的 web.config 中也有以下几行。
<authentication mode="Windows" />
<authorization>
<deny users="?"/>
</authorization>
<providers>
....
<add name="WindowsProvider" type="System.Web.Security.WindowsTokenRoleProvider" />
</providers>
我看过的每篇文章都建议做我已经做过的步骤,但我仍然没有运气。
解决方案
用这个检查一下
string userName = System.Web.HttpContext.Current.User.Identity.Name
或者
User.Identity.Name
推荐阅读
- r - 在 AWS EC2 中将 Rstudio 与 PostgreSQL 连接
- graphql - 查询变量未从 apollo 中的 vue 组件传递
- javascript - 如何从 Firebase 云功能中获取 Firebase 用户 ID
- powershell - 为什么我从开始作业与仅运行代码在 Powershell 中得到不同的输出?
- prolog - 什么是统一术语顺序有关系吗?
- angular - Angular Material Drag & Drop - 重复主框图像
- python - pyplot.bar 用于长尺度
- spring - 需要一个找不到的 'org.springframework.security.authentication.AuthenticationManager' 类型的 bean。春天安全的消息
- python - 如何在 Kivy 的特定屏幕上通过下拉菜单构建?
- jquery - 拖放不断打开选项卡中的图像