【发布时间】:2011-11-08 17:00:22
【问题描述】:
我一直在努力让 Windows 身份验证与我正在开发的 MVC3 应用程序 (VS 2010 SP1) 一起工作,但到目前为止还没有真正的运气。我在我的开发机器上运行 XP(直到明年才能升级),如果这有什么不同的话。我还应该提到我使用 IIS Express 作为 VS 2010 的默认 Web 开发服务器。
前段时间我从一个空的 MVC3 应用程序开始,因为在我昨天升级 MVC3 工具之前,我没有从 VS 菜单中获得 Intranet 模板(好吧,我有点慢)。安装并检查该模板后,我向我的项目添加了对 DirectoryServices 的引用,并添加了在页面右上角显示当前登录用户的代码位。根据图形,它现在应该说“Welcome PCE\dnewman!”
我遵循了这篇文章中的出色说明:IIS Express Windows Authentication,并且确实取得了一些进展。但是,我现在从 IIS Express 收到 401.2 错误,告诉我我没有被授权...
所以,我进入我的项目属性并将匿名身份验证设置为启用。现在我可以访问网页了,但我在页面右上角看到的是“欢迎!” -- 没有用户名。
我在 IE 和 Firefox 上都试过了,结果相同。 $#^&!我在这里失踪了吗?看来我没有登录到Windows!我需要从哪里开始寻找问题?
几年前,我在以前的雇主那里编写了一个 Windows 窗体应用程序,该应用程序使用他们的 Windows 登录凭据对用户进行身份验证。在这种情况下,我必须特别注意发送凭据,然后在服务端显式验证它们。我必须做些什么才能使浏览器在每个 GET 或 POST 中都包含凭据?
根据要求,这是我的 web.config 的内容,对所有 Glimpse 配置内容表示歉意。
<?xml version="1.0"
encoding="utf-8"?>
<configuration>
<configSections>
<section name="glimpse"
type="Glimpse.Core.Configuration.GlimpseConfiguration" />
</configSections>
<appSettings>
<add key="ClientValidationEnabled"
value="true" />
<add key="UnobtrusiveJavaScriptEnabled"
value="true" />
</appSettings>
<system.web>
<compilation debug="true"
targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
<add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
<pages>
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.WebPages" />
</namespaces>
</pages>
<trace writeToDiagnosticsTrace="true"
enabled="true"
pageOutput="false" />
<httpModules>
<add name="Glimpse"
type="Glimpse.Core.Module" />
</httpModules>
<httpHandlers>
<add path="glimpse.axd"
verb="GET,POST"
type="Glimpse.Core.Handler" />
</httpHandlers>
</system.web>
<system.webServer>
<security>
<authentication>
<anonymousAuthentication enabled="false"/>
<windowsAuthentication enabled="true" />
</authentication>
</security>
<modules runAllManagedModulesForAllRequests="true">
<add name="Glimpse"
type="Glimpse.Core.Module,Glimpse.Core"
preCondition="integratedMode" />
</modules>
<validation validateIntegratedModeConfiguration="false" />
<handlers>
<add name="Glimpse"
path="glimpse.axd"
verb="GET,POST"
type="Glimpse.Core.Handler,Glimpse.Core"
preCondition="integratedMode" />
</handlers>
</system.webServer>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IReporting"
maxBufferSize="1024000"
maxBufferPoolSize="1000000"
maxReceivedMessageSize="1024000">
<readerQuotas maxDepth="200"
maxStringContentLength="65536"
maxArrayLength="32768"
maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
<security mode="None" />
</binding>
<binding name="normalBinding">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint name="tcpAdminServiceEndpoint"
address="net.tcp://PCESRV22.pce.local:9000/ProductionMonitor/AdminService"
binding="netTcpBinding"
bindingConfiguration="normalBinding"
contract="Contracts.IAdmin" />
<endpoint name="tcpMasterDataServiceEndpoint"
address="net.tcp://PCESRV22.pce.local:9010/ProductionMonitor/MasterDataService"
binding="netTcpBinding"
bindingConfiguration="normalBinding"
contract="Contracts.IMasterData" />
<endpoint name="tcpReportingServiceEndpoint"
address="net.tcp://PCESRV22.pce.local:9030/ProductionMonitor/ReportingService"
binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_IReporting"
contract="Contracts.IReporting" />
</client>
</system.serviceModel>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc"
publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0"
newVersion="3.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<glimpse enabled="true" />
</configuration>
我非常愿意根据需要透露更多细节,但我什至不知道从哪里开始寻找。我能找到的每篇文章或文章似乎都假设这部分只是作为 Windows/MVC3/ASP.NET 设置的一部分发生的。
谢谢,戴夫
【问题讨论】:
-
您是否禁用了此答案中提到的匿名身份验证? stackoverflow.com/questions/4762538/…
-
我支持禁用匿名身份验证。你提到你正在使用 IIS express?如果调试应用程序会发生什么?在你的构造函数上放一个断点,让我知道你在
this.User.Identity中找到了什么 -
至于第一条评论,是的,我做到了。我上面提到的同一篇文章也提到了这一点。当这不起作用时,我重新启用它以查看会发生什么并获得空白用户名。当我在启用匿名的情况下对其进行调试并查看 this.User.Identity 时,我从观察中得到了我期望的结果,即一个空字符串。当我禁用匿名身份验证时,应用程序在到达断点之前因 401.2 错误而死(在 Global.asax.cs 中的 Application_Start 处)
-
顺便说一句,我需要启用匿名身份验证,因为我只想保护站点中的某些页面。该网站的大部分内容需要对所有人开放。
-
@Dave 你的 Web.config 中有
<authentication mode="Windows" />吗?
标签: asp.net asp.net-mvc-3 windows-authentication