WebApi使用EF,Windows身份验证失败

时间:2013-06-20 12:03:19

标签: sql-server entity-framework asp.net-web-api

我使用WebApi构建了一个RESTful Web服务。我正在使用Entity Framework连接到数据库。 Localy工作正常。当我将网站上传到主机提供商并发出请求时,我会收到401结果以及以下标题:

HTTP/1.1 401 Unauthorized
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM
X-Powered-By: ASP.NET
X-Powered-By-Plesk: PleskWin
Date: Thu, 20 Jun 2013 11:28:47 GMT
Content-Length: 0
Proxy-Support: Session-Based-Authentication

第一件奇怪的事情是,如果不使用EF,我会使用经典的SqlConnectionSqlCommand。 第二个奇怪的是,如果我运行本地项目并在连接字符串中我连接到远程服务器,它也可以工作!

虽然刚来找我。可能是我的连接字符串是这样的,使用System.Data.SqlClient作为提供者吗?

<add name="MyContext" connectionString="Data Source=(local);Initial Catalog=MyDB;User Id=user;Password=password;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />

我在这个SO问题中尝试了一些建议,但没有任何效果。 Disable Windows Authentication for WebAPI

有什么建议吗?我已经在这3天了,实现了HttpModules,自定义属性,阅读有关处理程序的内容。我现在真的不知所措。

1 个答案:

答案 0 :(得分:1)

从WWW-Authenticate标题中可以看出,Windows身份验证的情况显而易见。

WWW-Authenticate: Negotiate
WWW-Authenticate: NTLM

您必须禁用Windows身份验证。如果您可以使用IIS管理器,请确保身份验证设置如下。

enter image description here

或者你可以使用web.config(假设集成了IIS 7)。

<system.webServer>
  <security>
    <authentication>
      <anonymousAuthentication enabled="false" />
      <basicAuthentication enabled="false" />
      <windowsAuthentication enabled="false" />
    </authentication>
  </security>
  ...
<system.webServer>
相关问题