好的,我很难找到这个问题,因为它在本地工作,但是在发布之后,结果很简单:
错误代码: 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 }/{action})。
// 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禁忌。
我毫无头绪。我甚至尝试将“网络服务”的整个发布文件夹的安全设置更改为完全访问。没有变化。
如果你还需要情报就告诉我。
发布于 2015-05-27 10:38:01
名为web服务器机器的家伙,他们有防火墙阻止传入的webapi呼叫并进行身份验证。它现在的工作方式是:)
https://stackoverflow.com/questions/30454519
复制相似问题