2017-01-28 1 views
0

다음 튜토리얼을 따라 내 푸른 하늘의 백엔드를 설정했습니다.C# Azure/Postman - 405 POST에서 허용되지 않는 메소드 오류

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

것은 그때 처음으로 우체부를 설치하고 설정합니다

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

{ "이름": "아드리안", "비밀번호": "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 
+0

당신이 당신의 요청에 콘텐츠 형식을 전달하는이 경우 통화에서

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 

:이 경우에는 다음과 같이

푸른 로그가 보인다? –

+0

GET과 관련된 두 가지 오류가 있습니다. Content-Type = "application/json"으로 POST 요청을 보내면 어떤 오류가 발생합니까? –

답변

0

상관의 요청을 만들 때 당신도 우체부에 ContentType = "application/json"을 설정하거나하는 요청의 헤더에 있는지 확인 고객.

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

에 컨트롤러 정의를 변경하고 단지 시험,

[System.Web.Http.HttpPost] 
[Route("post")] 
public IHttpActionResult Post([FromBody] User body) 

같은 POST에 대한 경로를 소개하고

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

에 요청을 시도

+0

은 이미 헤더에 application/json으로 설정되어 있습니다. –

+0

업데이트 된 답변을 사용해보십시오. –

+0

시도했는데 같은 오류가 발생했습니다 –

0

GET 대신 POSTMan에서 POST 요청을하고 싶을 것입니다. 액션에 [HttpGet]을 추가하지 마십시오. 메소드를 POSTMan의 POST로 설정하면됩니다.

그리고 우편함에 Content-Type: application/json 헤더를 설정했는지 확인하십시오.

+0

을 이미 사용하고 있다면'AllowAnonymous ''를 POST에 추가하십시오. 문제가 지속됩니다. –

0

Azure 서비스 API에서 [HttpPost]가 있는지 확인하십시오. 본문 매개 변수를 전송할 수있는 경우 올바른 API를 호출하는 동안 우편 발송자에서 게시를 선택했음을 의미합니다. 얻지 않기 위하여 우체부 외침 또는 푸른 api의 어느 쪽이든을 바꾸지 말라. 그것은 불일치 일 것이다.

azure 로그에 액세스 할 수있는 경우 http 포스트가 https url로 Get으로 리디렉션되는지 확인합니다.이 경우 https를 직접 호출 해보십시오.https://xxx.azurewebsites.net/api/Data/test

관련 문제