ASP.Net MVC 中的 User.Identity.Name 为空 [英] User.Identity.Name blank in ASP.Net MVC

查看:39
本文介绍了ASP.Net MVC 中的 User.Identity.Name 为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如标题一样.

仅在我的 HomeController 中:

Simply in my HomeController I have:

string Username = User.Identity.Name;

为什么这个值总是空白?

Why is this value always blank?

我必须在 web.config 中放置一些特别的东西来获得这个值.我已经在 VS 开发 Web 服务器以及 Windows Server 2003 IIS 服务器上尝试过.

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.

它必须是简单的东西,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.

非常感谢

推荐答案

当然是伙计.您需要通过网站进行身份验证.这是用于身份验证的名称.

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

您正在验证,对吗?

这不是设置等

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

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

那么您所追求的是 Windows 身份验证.A 快速谷歌搜索 想出了 这篇文章.它非常有用(虽然有点旧,但仍然相关)......检查一下.

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

找到更好的帖子,其中包含适用于 Windows 的 MVC 代码验证.来看看吧.

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

重要的配置设置是...

Config setting that is important, is...

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

这篇关于ASP.Net MVC 中的 User.Identity.Name 为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆