为什么HttpContext.Current.User.Identity.Name返回空白

时间:2022-09-10 18:27:34

I created a site. It is an internal site. It is a .NET 4.0 site. I would like to grab the username using HttpContext.Current.User.Identity.Name of the person browsing the site. There isnt a login page since it is internal. I am not sure what I am missing. Do I need the following in my webconfig:

我创建了一个网站。这是一个内部网站。它是一个.NET 4.0站点。我想使用浏览网站的人的HttpContext.Current.User.Identity.Name来获取用户名。没有登录页面,因为它是内部的。我不确定我错过了什么。我的webconfig中是否需要以下内容:

<authentication mode="Windows"></authentication>
<identity impersonate="true"/>
<authorization>
  <allow users="?"/>
</authorization>

and this:

和这个:

  <system.webServer>
  <validation validateIntegratedModeConfiguration="false" />

5 个解决方案

#1


15  

It comes up blank when unauthenticated. You can verify this by also doing:

未经身份验证时,它会显示为空白。你可以通过以下方式验证这一点:

HttpContext.Current.User.Identity.IsAuthenticated

Check your authentication level in IIS, is it set to enable windows authentication there too?

检查IIS中的身份验证级别,是否设置为启用Windows身份验证?

#2


8  

In iis disable anonymous access and enable integrated windows security for you web application.

在iis中禁用匿名访问并为您的Web应用程序启用集成的Windows安全性。

#3


4  

Yes, you do need, at least the

是的,你确实需要,至少是

<authentication mode="Windows"></authentication>

part is required if you are using Integrated Windows Auth.

如果您使用集成Windows身份验证,则需要部分。

After that, you should be able to grab the username of the person logged in.

之后,您应该能够获取登录用户的用户名。

No, you don't need:

不,你不需要:

<validation validateIntegratedModeConfiguration="false" />

#4


2  

In IIS, go to Sites \ Default Website \ [Your Website].

在IIS中,转到Sites \ Default Website \ [Your Website]。

Select Authentication option and disable Anonymous Authentication.

选择身份验证选项并禁用匿名身份验证。

#5


1  

In my case removing <remove name="FormsAuthentication" /> line from web.config helps.

在我的例子中,从web.config中删除 行有帮助。

I also have <authentication mode="Forms" /> in <system.web> section.

我在 部分中也有

#1


15  

It comes up blank when unauthenticated. You can verify this by also doing:

未经身份验证时,它会显示为空白。你可以通过以下方式验证这一点:

HttpContext.Current.User.Identity.IsAuthenticated

Check your authentication level in IIS, is it set to enable windows authentication there too?

检查IIS中的身份验证级别,是否设置为启用Windows身份验证?

#2


8  

In iis disable anonymous access and enable integrated windows security for you web application.

在iis中禁用匿名访问并为您的Web应用程序启用集成的Windows安全性。

#3


4  

Yes, you do need, at least the

是的,你确实需要,至少是

<authentication mode="Windows"></authentication>

part is required if you are using Integrated Windows Auth.

如果您使用集成Windows身份验证,则需要部分。

After that, you should be able to grab the username of the person logged in.

之后,您应该能够获取登录用户的用户名。

No, you don't need:

不,你不需要:

<validation validateIntegratedModeConfiguration="false" />

#4


2  

In IIS, go to Sites \ Default Website \ [Your Website].

在IIS中,转到Sites \ Default Website \ [Your Website]。

Select Authentication option and disable Anonymous Authentication.

选择身份验证选项并禁用匿名身份验证。

#5


1  

In my case removing <remove name="FormsAuthentication" /> line from web.config helps.

在我的例子中,从web.config中删除 行有帮助。

I also have <authentication mode="Forms" /> in <system.web> section.

我在 部分中也有