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

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

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

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

然后我第一次安装了 postman 并设置:

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

{“用户名”:“阿德里安”,“密码”:“ super secret ”}

Json(应用程序/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

最佳答案

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

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

关于C# Azure/Postman - POST 上不允许出现 405 方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41915252/

相关文章:

c# - 为什么 roslyn 对于未签名的引用给出警告 CS8002 而不是错误 CS1577?

c# - 我可以将 AsyncLocal 与 azure 函数一起使用吗?

c# - Visual Studio 重构快速操作和重构错误

python - 如何不断检查 JSON 文件是否在 Python 中更新?

php - 远程 json 读取在 jquery 中不起作用

c# - 根据提供的日期参数减去天数

c# - 使用 C# 加密并使用 OpenSSL 解密

json - 如何根据 XML 架构 (XSD) 或 RelaxNG 验证 JSON?

postgresql - azure + postgresql : How to fix error: Unable to connect to server: server closed the connection unexpectedly

azure - 加密 Web.config Windows Azure