I am receiving a 404: not found error when I try to call HasAccess. I use HttpPost to be able to pass a User class because it contains / and \\ symbols in some of the strings. This worked as a get but was unable to function because of the extra slashes, so I need to figure out how to make it work as a post or find another way around the \\ and / restriction. Here's the response message:
{Method: POST, RequestUri: 'http://localhost/WebServices/Security/api/SecurityCheck/HasAccess/',
Version: 1.1,
Content: System.Net.Http.ObjectContent`1[[Authorization.Models.User, Authorization, Version=1.0.3.0, Culture=neutral, PublicKeyToken=null]],
Headers:
{
Accept: application/json
Content-Type: application/json; charset=utf-8
Content-Length: 118
}}
Here is the function declaration in Web API
[Route("api/SecurityCheck/HasAccess/")]
[HttpPost]
public bool HasAccess(User current)
{
if (ValidateUser(current))
{
CurrentUser = current;
return AzMan.Roles.Count != 0;
}
return false;
}
My Route Configuration in Global.asax
GlobalConfiguration.Configure(config =>
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultAPI",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional, action = RouteParameter.Optional });
});
and my call to the Web API function from C#:
public async Task executeAsyncPost(string method, int? operationID = null)
{
try
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(ConfigurationSettings.AppSettings["SecurityCheckAPI"]);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
var user = new User();
user.UserName = User;
user.Application = Application;
user.AuthorizationStore = AuthorizationStore;
string message = "api/SecurityCheck/" + method + "/";
if (operationID.HasValue)
{
message += operationID.Value.ToString() + "/";
}
HttpResponseMessage response = await client.PostAsJsonAsync(message, user);
if(response.IsSuccessStatusCode)
{
switch(method)
{
case "GetOperationIds":
_operationIds = await response.Content.ReadAsAsync<int[]>();
break;
case "GetOperationNames":
_operationNames = await response.Content.ReadAsAsync<string[]>();
break;
case "CheckAccess":
_checkAccess = await response.Content.ReadAsAsync<bool>();
break;
case "HasAccess":
_hasAccess = await response.Content.ReadAsAsync<bool>();
break;
case "GetRoles":
_roles = await response.Content.ReadAsAsync<object[]>();
break;
}
}
}
}
catch (Exception ex)
{
throw ex;
}
}
删除[Route("api/SecurityCheck/HasAccess/")]
上的最后一个/以及RequestUri
本身。
It was missing a / at the end of the request Uri. The lack of that / actually removed the entire authorization section from the path and led directly into api.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.