Azure应用服务 – 自定义身份validation – 不允许HTTP动词

我按照本教程在我的Xamarin.Forms应用程序中启用身份validation: 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 Verb错误消失但发生以下错误:

  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.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.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.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) 

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

我发现Azure SDK 在调用LoginAsync时发送POST和GET请求

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

在此处输入图像描述

正如此问题所述,从http – > https重定向会导致http动词变为GET。

在初始化MobileServiceClient ,使用Https更改移动应用程序Uri,您的代码将按预期工作。

在此处输入图像描述