C#Azure / Postman - POST方法不允许405方法错误

时间:2017-01-28 21:52:38

标签: c# json azure http-status-code-405

我按照以下教程设置了我的后端。

https://adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/chapter2/custom/

然后我第一次安装邮差并设置:

http://apptest.azurewebsites.net/.auth/login/custom

{“username”:“adrian”,“password”:“supersecret”}

Json(application / json)

但是,我一直收到这个错误:

405方法不允许

{
  "Message": "The requested resource does not support http method 'GET'."
}

后端代码:

using System;
using System.IdentityModel.Tokens;
using System.Linq;
using System.Security.Claims;
using System.Web.Http;
using Newtonsoft.Json;
using AppTestService.Models;
using AppTestService.DataObjects;

using Microsoft.Azure.Mobile.Server.Login;


namespace AppTestService.Controllers
{
    [Route(".auth/login/custom")]
    public class CustomAuthController : ApiController
    {
        private AppTestContext db;
        private string signingKey, audience, issuer;

        public CustomAuthController()
        {
            db = new AppTestContext();
            signingKey = Environment.GetEnvironmentVariable("WEBSITE_AUTH_SIGNING_KEY");
            var website = Environment.GetEnvironmentVariable("WEBSITE_HOSTNAME");
            audience = $"https://{website}/";
            issuer = $"https://{website}/";
        }

        [System.Web.Http.HttpPost]
        public IHttpActionResult Post([FromBody] User body)
        {
            if (body == null || body.Username == null || body.Password == null ||
                body.Username.Length == 0 || body.Password.Length == 0)
            {
                return BadRequest();
            }

            if (!IsValidUser(body))
            {
                return Unauthorized();
            }

            var claims = new Claim[]
            {
                new Claim(JwtRegisteredClaimNames.Sub, body.Username)
            };

            JwtSecurityToken token = AppServiceLoginHandler.CreateToken(
                claims, signingKey, audience, issuer, TimeSpan.FromDays(30));
            return Ok(new LoginResult()
            {
                AuthenticationToken = token.RawData,
                User = new LoginResultUser { UserId = body.Username }
            });
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool IsValidUser(User user)
        {
            return db.Users.Count(u => u.Username.Equals(user.Username) && u.Password.Equals(user.Password)) > 0;
        }


    }


    public class LoginResult
    {
        [JsonProperty(PropertyName = "authenticationToken")]
        public string AuthenticationToken { get; set; }

        [JsonProperty(PropertyName = "user")]
        public LoginResultUser User { get; set; }
    }

    public class LoginResultUser
    {
        [JsonProperty(PropertyName = "userId")]
        public string UserId { get; set; }
    }


}

如果我在函数顶部添加[System.Web.Http.HttpGet],那么我会得到一个不同的错误。 :

415不支持的媒体类型

{
  "Message": "The request contains an entity body but no Content-Type header. The inferred media type 'application/octet-stream' is not supported for this resource."
}

这些是标题:

Allow →POST
Content-Length →72
Content-Type →application/json; charset=utf-8
Date →Sat, 28 Jan 2017 22:08:48 GMT
Server →Microsoft-IIS/8.0
X-Powered-By →ASP.NET

3 个答案:

答案 0 :(得分:0)

请确保在您的请求标题中设置邮递员中的.386 .model flat, stdcall option casemap:none includelib C:\masm32\lib\kernel32.lib .data .code start: ExitProcess PROTO STDCALL :DWORD invoke ExitProcess,0 end start ,或者甚至在从任何客户端创建请求时。

将控制器定义更改为

ContentType = "application/json"

只是为了测试,在POST上引入路线,如

[RoutePrefix("auth/login/custom")]
public class CustomAuthController : ApiController
{
}

并尝试向

发出请求
  

http://apptest.azurewebsites.net/auth/login/custom/post

答案 1 :(得分:0)

您可能希望从PostMan而不是GET发出POST请求。不要在操作上添加[HttpGet],只需将方法设置为PostMan中的POST。

并确保在PostMan中设置标题Content-Type: application/json

答案 2 :(得分:0)

在Azure服务API中确保它与[HttpPost]一起使用。 如果您能够发送正文参数,则意味着您在调用API时选择了Post in post,这是正确的。不要改变邮递员电话或azure api。这将是不匹配的。

如果您有权访问azure日志,请检查http Post是否被重定向到https url作为Get,在这种情况下,请尝试直接调用https。

在这种情况下,

Azure日志如下所示:

Received request: POST http://xxx.azurewebsites.net/api/Data/test
Information Redirecting: https://xxx.azurewebsites.net/api/Data/test
Received request: GET https://xxx.azurewebsites.net/api/Data/test

在这种情况下请致电https://xxx.azurewebsites.net/api/Data/test