好吧,我很难找到问题,因为它在本地工作,但在发布后,结果很简单:
错误代码:403禁止.服务器拒绝指定的统一资源定位符(URL).联系服务器管理员. (12202)
代码:
[RoutePrefix("api/v1/project")] public class ProjectController : BaseApiController { [HttpGet] public HttpResponseMessage GetProjects() { HttpResponseMessage resp = new HttpResponseMessage(HttpStatusCode.OK); if(User.Identity.IsAuthenticated) { var model = new ModelFactory().CreateProjects(); resp = Request.CreateResponse(HttpStatusCode.OK,model); } return resp; } }
public static class WebApiConfig { public static void Register(HttpConfiguration config) { // all actions under /project routes require authentication config.Routes.MapHttpRoute( name: "ProjectApi",routeTemplate: "api/v1/{controller}/{action}/{apikey}",defaults: new { apikey = RouteParameter.Optional },constraints: new { controller = "project" },handler: new BasicAuthHandler(config)); // all routes requires an api key config.MessageHandlers.Add(new ApiKeyHandler()); config.MapHttpAttributeRoutes(); } }
我已经从网上尝试了几个“解决方案”,但似乎没有一个解决这个问题.我添加了:
// Stop IIS/Asp.Net breaking our routes RouteTable.Routes.RouteExistingFiles = true;
来自:http://www.grumpydev.com/2013/09/17/403-14-error-when-trying-to-access-a-webapi-route/
并确保:
<modules runAllManagedModulesForAllRequests="true">
拥有上面的代码,使用以下链接提供了一个成功的连接,它检查(按正确的顺序)APIkey(ApiKeyHandler),检查用户是否需要登录(BasicAuthHandler)然后转到控制器中的方法({controller }/{行动}).
// THIS WORKS! http://localhost:51077/api/v1/project/getprojects?apikey=123456
然后我们做一个发布并尝试相同的事情
// This is haunted with number 403 http://website.com/api/v1/project/getprojects?apikey=123456
给出错误代码:403 Forbidden.
我很无能为力.我甚至尝试将“NETWORK SERVICE”的整个发布文件夹的安全设置更改为完全访问权限..没有变化.
如果您需要更多英特尔,请告诉我.
解决方法
称为Web服务器计算机,他们有一个防火墙阻止传入的webapi调用进行身份验证.它现在应该工作:)