Identity.Name 为空

Identity.Name is null

我的 MVC 应用程序,

装饰控制器

[AuthorizedRoles(Roles = "Manager")]

我正在尝试使用 HttpContextBase 获取请求的用户名

  public class AuthorizedRoles : AuthorizeAttribute
  {
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
       httpContext.User.Identity.Name

为什么 Identity.Name 为空?

Web.config 长得像

<authentication mode="Windows" />
<authorization>
  <allow users="*"/>
</authorization>

请帮助获取请求用户..

好的,这就是你需要做的!

我不知道要在 web.config 中设置什么 - 但更改这些选项可以使它起作用。要获得此屏幕 select,请投影并按 'F4'

编辑:我想您在托管网站时也需要在 IIS 中进行配置。 这可能有助于 web.config

  <system.webServer>
    ....
    <security>
      <authentication >
        <anonymousAuthentication enabled="false"/>
        <windowsAuthentication enabled="true"/>
      </authentication>
    </security>
  </system.webServer>