asp.net-mvc ASP.Net MVC 中的 User.Identity.Name 为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1067591/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-07 23:42:17  来源:igfitidea点击:

User.Identity.Name blank in ASP.Net MVC

asp.net-mvciis

提问by Aaron

Exactly as per the title.

完全按照标题。

Simply in my HomeController I have:

只需在我的 HomeController 中,我就有:

string Username = User.Identity.Name;

Why is this value always blank?

为什么这个值总是空白?

Is there something special I have to place in the web.config to get this value to come through. I've tried on both the VS dev web server as well as on a windows server 2003 IIS server.

我必须在 web.config 中放置一些特殊的东西来获得这个值。我已经在 VS dev web 服务器和 windows server 2003 IIS 服务器上尝试过。

It's got to be something simple, a server setting in IIS or something as the code is so simple and seems to be the correct way to reference this value.

它必须是简单的东西,IIS 中的服务器设置或其他东西,因为代码非常简单,似乎是引用此值的正确方法。

Thx a lot

多谢

采纳答案by Pure.Krome

Sure is mate. You need to authenticate with the website. That's the name used for authentication.

当然是队友。您需要通过网站进行身份验证。这是用于身份验证的名称。

You are authenticating, right?

你正在认证,对吧?

It's not a setting, etc.

这不是设置等。

Click the LOG IN link, if you're using the stock standard ASP.NET MVC template (if my memory serves me right).

单击登录链接,如果您使用的是标准 ASP.NET MVC 模板(如果我没记错的话)。

UPDATE (as the posted has added more info/comments)

更新(因为已发布添加了更多信息/评论)

So what you're after is Windows Authentication. Aquick google searchcame up with this post. It's pretty helpful (though a bit old, but still relevant) .. check that out.

所以你所追求的是 Windows 身份验证。一个快速谷歌搜索想出了这个职位它非常有用(虽然有点旧,但仍然相关)..检查一下。

Found a better postwith MVC code for Windows Authentication. Check that out instead.

找到了一个更好的帖子,其中包含用于 Windows 身份验证的 MVC 代码。请检查一下。

Config setting that is important, is...

重要的配置设置是...

...
 <system.web>
  ...
  <authentication mode="Windows"/>
  ...
 </system.web>
 ...

回答by Pricey

If you are wanting to use windows authentication it's not enough to just add

如果您想使用 Windows 身份验证,仅添加是不够的

...
 <system.web>
  ...
  <authentication mode="Windows"/>
  ...
 </system.web>
 ...

You also want to check the readme file instructions for the MVC project, especially if your running in IIS Express and using Visual Studio 2012.

您还需要查看 MVC 项目的自述文件说明,特别是如果您在 IIS Express 中运行并使用 Visual Studio 2012。

See below:

见下文:

Hosting on IIS Express:

  1. Click on your project in the Solution Explorer to select the project.

  2. If the Properties pane is not open, open it (F4).

  3. In the Properties pane for your project:
    a) Set "Anonymous Authentication" to "Disabled".
    b) Set "Windows Authentication" to "Enabled".

Hosting on IIS 7 or later:

  1. Open IIS Manager and navigate to your website.

  2. In Features View, double-click Authentication.

  3. On the Authentication page, select Windows authentication. If Windows authentication is not an option, you'll need to make sure Windows authentication is installed on the server.

    To enable Windows authentication on Windows:

a) In Control Panel open "Programs and Features". b) Select "Turn Windows features on or off".

c) Navigate to Internet Information Services > World Wide Web Services > Security and make sure the Windows authentication node is checked.

To enable Windows authentication on Windows Server:

a) In Server Manager, select Web Server (IIS) and click Add Role Services.

b) Navigate to Web Server > Security and make sure the Windows authentication node is checked.

  1. In the Actions pane, click Enable to use Windows authentication.

  2. On the Authentication page, select Anonymous authentication.

  3. In the Actions pane, click Disable to disable anonymous authentication.

在 IIS Express 上托管:

  1. 在解决方案资源管理器中单击您的项目以选择该项目。

  2. 如果“属性”窗格未打开,请将其打开 (F4)。

  3. 在项目的“属性”窗格中:
    a) 将“匿名身份验证”设置为“已禁用”。
    b) 将“Windows 身份验证”设置为“已启用”。

在 IIS 7 或更高版本上托管:

  1. 打开 IIS 管理器并导航到您的网站。

  2. 在功能视图中,双击身份验证。

  3. 在身份验证页面上,选择 Windows 身份验证。如果 Windows 身份验证不是一个选项,您需要确保在服务器上安装了 Windows 身份验证。

    在 Windows 上启用 Windows 身份验证:

a) 在控制面板中打开“程序和功能”。b) 选择“打开或关闭 Windows 功能”。

c) 导航到 Internet 信息服务 > 万维网服务 > 安全性并确保选中 Windows 身份验证节点。

在 Windows Server 上启用 Windows 身份验证:

a) 在服务器管理器中,选择 Web 服务器 (IIS) 并单击添加角色服务。

b) 导航到 Web 服务器 > 安全并确保选中 Windows 身份验证节点。

  1. 在操作窗格中,单击启用以使用 Windows 身份验证。

  2. 在身份验证页面上,选择匿名身份验证。

  3. 在操作窗格中,单击禁用以禁用匿名身份验证。

回答by anonymous

If you do not wish to Authorizeon every controller or action, you can do a blanket authorization in the web.config file. This should work as long as you are using Windows authentication. If you allow ASP.NET to control the authentication, then you would not need to configure any IIS setting. It should then work well with whatever web server that you are running on. I do not know or assume what u have tried so far I'll try to be complete in my answer. First remark off the forms authentication tag in web.config. All following settings are placed in the system.webconfiguration section.

如果您不希望对每个控制器或操作进行授权,您可以在 web.config 文件中进行全面授权。只要您使用 Windows 身份验证,这应该有效。如果允许 ASP.NET 控制身份验证,则不需要配置任何 IIS 设置。然后它应该可以很好地与您运行的任何 Web 服务器配合使用。我不知道或假设你到目前为止已经尝试过什么,我会尽量在我的回答中完成。首先注释掉 web.config 中的表单身份验证标记。以下所有设置都放在system.web配置部分。

<!--
    <authentication mode="Forms">
      <forms loginUrl="~/Account/LogOn" timeout="2880" />
    </authentication>
-->

Replace with the Windows authentication tag.

替换为 Windows 身份验证标记。

<authentication mode="Windows" />

Then add the authorization tag to deny access to anonymous users. If the users are using Internet Explorer and are connecting from an Intranet zone, IE automatically will login the user. But if the user is connecting from the Internet zone, IE will still display a login box though for safety. But these are options that can be set from IE.

然后添加授权标签以拒绝匿名用户的访问。如果用户使用 Internet Explorer 并从 Intranet 区域连接,IE 将自动登录用户。但如果用户是从 Internet 区域连接,IE 仍会显示一个登录框,但为了安全。但这些是可以从 IE 设置的选项。

<authorization>
    <deny users="?" />
</authorization>

Setting authentication mode alonewithout authorization does not force the user to be authenticated in ASP.NET. If you want to control the security from IIS, I cannot help much with IIS settings but I know basically you can disable Basic Authentication , then enable Integrated Windows Authentication and then disable the Anonymous Login Account which will achieve the same or better results.

未经授权单独设置身份验证模式不会强制用户在 ASP.NET 中进行身份验证。如果您想从 IIS 控制安全性,我对 IIS 设置无能为力,但我知道基本上您可以禁用 Basic Authentication ,然后启用 Integrated Windows Authentication ,然后禁用 Anonymous Login Account ,这将获得相同或更好的结果。

I am also working on an MVC project at the moment and I tested the above settings with my project and it works. You would not need the Authorizeattribute since we have the authorizationtag in the configuration. I hope this can be of help to you and not get another -1.

我目前也在一个 MVC 项目上工作,我用我的项目测试了上述设置,它工作正常。您不需要Authorize属性,因为我们在配置中有授权标签。我希望这可以对你有所帮助,而不是再得到 -1。

回答by anonymous

Have you attached Authorizeattribute to ur action or controller?

您是否已将Authorize属性附加到您的操作或控制器?

public class HomeController : Controller {
    [Authorize]
    public ActionResult Index() {
         string userName = User.Identity.Name;
         return View();
    }
}