我在Azure中使用.NET后端的Xamarin.Forms应用程序中实现自定义身份验证,某些控制器标有[授权]。我已经按照大量博客和文章的说明进行操作,但是仍然不清楚如何使未经授权的TableController调用触发登录屏幕,或者如何访问相同的身份验证子系统loginAsync来设置userId或令牌信息,以便MobileServiceClient的多个实例保留最新的userId和MobileServiceAuthenticationToken值。如何正确触发登录页面并更新MobileServiceClient与其他OOTB提供程序访问的同一身份验证子系统中的userId / token?
答案 0 :(得分:3)
When your mobile app tries to access a backend that is marked with [Authorize], but an authorization header is not provided (technically, an X-ZUMO-AUTH header with a valid JWT), the service returns 401 Unauthenticated. There are a number of ways to trap this, but your operation will fail (and in a Xamarin.Forms application, that will cause a MobileServiceInvalidOperationException). You need to handle this case, call loginAsync() and then re-try the operation.
You should not have multiple MobileServiceClients - you should have one at a global scope (use DI to inject it or just make it a static in the App context). The MobileServiceUser will be used by all tables that are generated.