Azure 应用服务 - 自定义身份验证 - 不允许使用 HTTP 动词

Azure App Service - Custom Authentication - HTTP Verb not allowed

我按照本教程在我的 Xamarin.Forms 应用程序中启用身份验证: https://adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/chapter2/custom/

使用 Postman 进行的测试(如教程中所述)成功进行。令牌已返回。

当我从我的 C# 代码调用它时...

LoginAsync("custom", Newtonsoft.Json.Linq.JObject.FromObject(auth));

我收到如下错误:

Method not allowed. HTTP Verb not allowed

我发现 Azure SDK 在调用 LoginAsync 时发送了一个 POST 和 GET 请求。所以我改变了这个...

[HttpPost, Route(".auth/login/custom")]
        public IHttpActionResult Post([FromBody]CPM.Arda.Mobile.Freelancer.Backend.DataObjects.Recruitment.Custom.PromoterAuthRequest promoterAuth)

对此...

[HttpPost, HttpGet, Route(".auth/login/custom")]
        public IHttpActionResult Post([FromBody]CPM.Arda.Mobile.Freelancer.Backend.DataObjects.Recruitment.Custom.PromoterAuthRequest promoterAuth)

HTTP 动词错误已消失,但出现以下错误:

 Operation=ReflectedHttpActionDescriptor.ExecuteAsync, Exception=System.NullReferenceException: Object reference not set to an instance of an object.
   at CPM.Arda.Mobile.Freelancer.Backend.Controllers.Custom.CustomAuthController.Post(PromoterAuthRequest promoterAuth)
   at lambda_method(Closure , Object , Object[] )
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Web.Http.Tracing.ITraceWriterExtensions.<TraceBeginEndAsyncCore>d__18`1.MoveNext()
2017-07-11T10:39:50  PID[9680] Error       Operation=ApiControllerActionInvoker.InvokeActionAsync, Exception=System.NullReferenceException: Object reference not set to an instance of an object.
   at CPM.Arda.Mobile.Freelancer.Backend.Controllers.Custom.CustomAuthController.Post(PromoterAuthRequest promoterAuth)
   at lambda_method(Closure , Object , Object[] )
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)
   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)

我真的很困惑。此主题没有可用的教程似乎开箱即用。

I found out that the Azure SDK sends a POST and GET request when calling LoginAsync.

据我所知,移动应用后端会强制使用 https,如果您使用 http 发送请求,那么您将获得如下 302 状态代码:

正如这个 issue 提到的,从 http -> https 重定向导致 http 动词 GET。

当您初始化 MobileServiceClient 时,将移动应用 Uri 更改为 Https,您的代码将按预期工作。