Azure App Service - Custom Authentication - HTTP Verb not allowed

回眸只為那壹抹淺笑 提交于 2019-12-09 03:44:37

问题


I followed this tutorial to enable authentication in my Xamarin.Forms App: https://adrianhall.github.io/develop-mobile-apps-with-csharp-and-azure/chapter2/custom/

A test with Postman (as described in the tutorial) went succesfully. The token returned.

When i call this from my C# code...

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

I got an error like:

Method not allowed. HTTP Verb not allowed

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

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

to this...

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

The HTTP Verb error is gone but the following error occurs:

 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'm really confused. No available tutorial for this topic seems to work out of the box.


回答1:


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

AFAIK, the mobile app backend would force https, and if you send request with http, then you would get a 302 status code as follows:

As this issue mentioned, redirecting from http -> https causes the http verb to GET.

Change the mobile app Uri with Https when you init the MobileServiceClient, your code would work as expected.



来源:https://stackoverflow.com/questions/45032255/azure-app-service-custom-authentication-http-verb-not-allowed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!