【问题标题】:ServiceStack 'Access is denied' again, and other issuesServiceStack '访问被拒绝' 再次和其他问题
【发布时间】:2013-09-30 18:32:00
【问题描述】:

我以为我已经解决了对我的 ServiceStack Web 服务 in this question 的访问问题,但我现在遇到了相同的“访问被拒绝”错误,即使在该解决方案中找到了修复程序并且无法解决他们是我自己的。

这就是我的看法: 我有一个网络服务,我正在使用 POST 方法向其发送数据。我还有一个 GET 方法,它返回与 POST 相同的响应类型,并且工作正常。据我所知,该请求在失败之前甚至没有到达 ServiceStack。响应中没有 Access-Control 标头,即使我在响应标头中使用了 CorsFeature 插件。我不明白为什么它没有进入任何 ServiceStack 代码......一切似乎都设置正确。顺便说一句,当我尝试删除操作时,我从服务器收到“403 Forbidden, Write access is denied”错误,如果这有帮助吗?

这是我的 Global.asax(相关部分):

public class AppHost : AppHostBase
{
    public AppHost() : base("RMS Citations Web Service", typeof(CitationService).Assembly) { }

    public override void Configure(Container container)
    {
        SetConfig(new EndpointHostConfig
        {
            DefaultContentType = ContentType.Json,
            ReturnsInnerException = true,
            WsdlServiceNamespace = "http://www.servicestack.net/types"
        });

        Plugins.Add(new CorsFeature());
        RequestFilters.Add((httpReq, httpRes, requestDto) =>
        {
            if (httpReq.HttpMethod == "OPTIONS")
                httpRes.EndRequestWithNoContent(); //   extension method                    
        });

        container.RegisterAutoWired<CitationRequest>();

        // Not sure I need this - is it necessary for the Funq container?
        using (var addCitation = container.Resolve<CitationService>())
        {
            addCitation.Post(container.Resolve<CitationRequest>());
            addCitation.Get(container.Resolve<CitationRequest>());
            addCitation.Delete(container.Resolve<CitationRequest>());
        }
    }
}

protected void Application_Start(object sender, EventArgs e)
{
    new AppHost().Init();
}

这是我的请求和响应类:

[Route("/citations", "POST, OPTIONS")]
[Route("/citations/{ReportNumber_Prefix}/{ReportNumber}/{AgencyId}", "GET, DELETE, OPTIONS")]
public class CitationRequest : RmsData.Citation, IReturn<CitationResponse>
{
    public CitationStatus Status { get; set; }
}

public enum CitationStatus
{
    COMP,
    HOLD
}

public class CitationResponse
{
    public bool Accepted { get; set; }
    public string ActivityId { get; set; }
    public int ParticipantId { get; set; }
    public string Message { get; set; }
    public Exception RmsException { get; set; }
}

这是我的服务类:

public class CitationService : Service
{
    public Repository Repository { get { return new Repository(); } }

    public CitationResponse Post(CitationRequest citation)
    {
        var response = new CitationResponse { Accepted = false };

        if (string.IsNullOrEmpty(citation.ReportNumber))
        {
            response.Accepted = false;
            response.Message = "Report number was empty, so no data was sent to the web service.";
            return response;
        }

        try
        {
            response.ActivityId = Repository.CreateCitation(citation.ReportNumber, citation.ReportNumber_Prefix, citation.ViolationDateTime, citation.AgencyId, citation.Status);
            response.Accepted = true;
        }
        catch (Exception ex)
        {
            response.Accepted = false;
            response.Message = ex.Message;
            response.RmsException = ex;
        }

        return response;
    }

    public CitationResponse Get(CitationRequest citation)
    {
        var citationResponse = new CitationResponse();
        if (string.IsNullOrEmpty(citation.ReportNumber))
        {
            citationResponse.Accepted = false;
            citationResponse.Message = "Error occurred passing citation data to web service.";
            return citationResponse;
        }

        var isDuplicate = Repository.IsDuplicateReportNumber(citation.AgencyId, citation.ReportNumber, citation.ReportNumber_Prefix);
        citationResponse = new CitationResponse
        {
            Accepted = isDuplicate,
            Message =
                isDuplicate ? "Report Number already exists in database." : "Report Number has not yet been used."
        };

        return citationResponse;
    }

    public CitationResponse Delete(CitationRequest citation)
    {
        var citationResponse = new CitationResponse();
        try
        {
            if (Repository.DeleteCitation(citation.ReportNumber, citation.AgencyId, citation.ReportNumber_Prefix))
            {
                citationResponse.Accepted = true;
                citationResponse.Message = "Citation removed from RMS successfully.";
            }
            else
            {
                citationResponse.Accepted = false;
                citationResponse.Message = "Citation NOT deleted from RMS.  Check exception for details.";
            }
        }
        catch (Exception ex)
        {
            citationResponse.Accepted = false;
            citationResponse.Message = ex.Message;
            citationResponse.RmsException = new Exception(ex.Message);
            throw;
        }

        return citationResponse;
    }
}

最后,这是我将数据发送到 Web 服务的方式。它总是直接进入错误块:

SendCitationToDb: function (cit, callback) {
    $.ajax({
        type: "POST",
        url: Citations.DataServiceUrl + "citations",
        data: JSON.stringify(cit),
        contentType: "application/json",
        dataType: "json",
        success: function(data) {
            if (!data.Accepted) {
                Citations.ShowMessage('Citation not added', 'Citation not added.  Error was: ' + data.Message, 'error');
            } else {
                ActivityId = data.ActivityId;
                callback(data);
            }
        },
        error: function(errMsg) {
            Citations.ShowMessage('Citation not added', 'Citation not added.  Error was: ' + errMsg.statusText, 'error');
        }
    });
}

以下是 Chrome 开发工具的示例输出。首先来自此服务(GET)的良好响应:

更新在 IIS 中将 aspnet_isapi.dll 路径添加到通配符后的响应::(已删除屏幕截图)

Update 2 - 这是 POST 响应。请求显示它是 POST 方法,但代码直接跳转到 jQuery ajax 函数中的错误块。在开发工具中,我看到了这一行:

然后我单击它并获取这些请求和响应标头:

不知道还要寻找什么 - 我知道它说状态代码 200 OK,但我认为这只是为了飞行前的 OPTIONS...?不确定,但它不会从服务返回有效响应。

非常感谢您的帮助!

【问题讨论】:

    标签: c# jquery web-services servicestack


    【解决方案1】:

    WebDav 似乎正在处理并拒绝您的请求。你可以试试disabling WebDav看看有没有帮助。

    【讨论】:

    • 谢谢@mythz - 刚刚尝试过,但我从服务器得到了相同的响应。我尝试过的链接上列出的唯一解决方案是 web.config 条目。不知道如何实施其他...?从响应来看,WebDAV 似乎没有被禁用,并且仍在处理请求......
    • 好的,我进去并在该服务器的 IIS 中手动禁止了 WebDAV。现在,当我运行相同的请求时,我仍然收到相同的错误,但该请求不包括 DAV 条目:Allow:OPTIONS, TRACE, GET, HEAD Content-Length:0 Date:Mon, 30 Sep 2013 18:57:50 GMT 公共:OPTIONS、TRACE、GET、HEAD、POST 服务器:Microsoft-IIS/6.0 X-Powered-By:ASP.NET
    • 如果响应头中不包含X-Powered-By: ServiceStack..,那么请求还没有到达ServiceStack,其他东西劫持了它。我会寻找与IIS6 DELETE 403 相关的问题,例如this
    • 好的,现在我们到了某个地方。我将我的 aspnet_isapi.dll 路径添加到 IIS 中的通配符部分,现在我的 DELETE 操作有效。不幸的是,我的 POST 仍然无法正常工作,但响应标头确实包含 ServiceStack,所以至少我正在访问代码。不过,根据回复无法说出更多其他信息……(请参阅上面的更新回复截图)
    • @Eddie 他们看起来都像200 OK?你有 POST 错误响应的截图吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-15
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 2015-06-23
    • 1970-01-01
    • 2013-05-11
    相关资源
    最近更新 更多