Wcf 数据服务 - 401 错误
我正在构建一个需要登录的公共网站,到目前为止我一直在使用 asmx-services。现在我想尝试 Wcf 数据服务并通过 jQuery 查询服务。
这一切在本地主机上运行良好,但是一旦我发布我的网站,我就会收到此错误:
401 - Unauthorized: Access is denied due to invalid credentials.
我已使用实体数据模型映射我的数据库,然后我创建了一个 wcf 数据服务,该服务连接到我的数据模型,如下所示:
public class MyService : DataService<MyEntities>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
}
}
其中指定 MyEntities将我的实体数据模型映射到我的数据库。
正如我所写,它在本地主机上一切正常,我可以像这样查询我的服务:
$.ajax({
type: "GET",
url: "/OData/MyService.svc/Groups",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
//some logic
}});
但是当我使用发布的版本时,我收到 401 错误。 (直接在浏览器中编写查询时,我遇到了同样的错误)
我真的很喜欢使用 Wcf 服务,所以我真的需要帮助才能完成这项工作,我尝试过 Google 但没有运气。我需要在我的服务、web.config 或 IIS 中进行任何配置吗?
我使用 IIS 7.0 和 asp.net 4.0
Im building a public web site where you need to be logged in and i have been using asmx-services up to now. Now i want to try Wcf data service instead and query the service via jQuery.
It all works fine on localhost but once i publish my web site i get this error:
401 - Unauthorized: Access is denied due to invalid credentials.
I have mapped my database using Entity Data Model and then i created a wcf data service which is connected to my Data Model like this:
public class MyService : DataService<MyEntities>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
}
}
where MyEntities is specified when mapping my Entity Data Model to my database.
As i wrote, it all works fine on localhost, i can query my service like this:
$.ajax({
type: "GET",
url: "/OData/MyService.svc/Groups",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
//some logic
}});
but when i use the published version I get the 401-error. (i get the same error when writing the query directly in the browser)
I'm really green using Wcf-services so i really need help to make this work, i have tried to Google but with no luck. Do i need to do any configuration in my service or in web.config or in the IIS?
Im using IIS 7.0 and asp.net 4.0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误 401 是 HTTP 错误 - 访问被拒绝。
这是在到达服务器端 C# 代码之前来自 IIS 的。
如何修复它取决于您的身份验证是什么。但基本思想是请求必须包含足够的身份验证信息才能让服务器接受该请求。
The error 401 is a HTTP error - Access denied.
This is coming from IIS before it reaches your server side c# code.
How to fix it depends on what your authentication is. But the basic idea is that the request must contain enough authentication information for the request to be accepted by the server.
我终于让它工作了,指定的用户没有磁盘的读取权限。有时看起来很难,但其实很简单。
案件结案。
I finally got it to work, the specified user did not have read permissions on the disk. Sometimes it can seem hard but be so simple.
Case closed.