Last active
December 10, 2015 10:38
-
-
Save joshtwist/4422104 to your computer and use it in GitHub Desktop.
Using a filter in a C# Windows 8 application to automatically log a user back into the Mobile Service if any request returns a 401 (for example, due to a timeout of the token).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // This is a modified App.xaml.cs file from the Mobile Services quickstart, showing how to add the AuthFilter above | |
| private static MobileServiceClient GetClient() | |
| { | |
| var client = new MobileServiceClient( | |
| "<your-app-url>", | |
| "<your-app-key>" | |
| ); | |
| var filter = new AuthFilter(); | |
| var filtered = client.WithFilter(filter); | |
| filter.Client = filtered; | |
| return filtered; | |
| } | |
| // This MobileServiceClient has been configured to communicate with your Mobile Service's url | |
| // and application key. You're all set to start working with your Mobile Service! | |
| public static MobileServiceClient MobileService = GetClient(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class AuthFilter : IServiceFilter | |
| { | |
| public MobileServiceClient Client { get; set; } | |
| private async System.Threading.Tasks.Task<IServiceFilterResponse> HandleAsync( | |
| IServiceFilterRequest request, | |
| IServiceFilterContinuation continuation) | |
| { | |
| IServiceFilterResponse response = await continuation.Handle( | |
| request).AsTask(); | |
| while (response.StatusCode == 401) | |
| { | |
| // oh noes, user is not logged in - we got a 401 | |
| // log them in, this time hardcoded Twitter but you | |
| // would trigger your login presentation in your application | |
| // first, we need to log the user in | |
| try | |
| { | |
| await Client.LoginAsync(MobileServiceAuthenticationProvider.Twitter); | |
| } | |
| catch (InvalidOperationException exc) | |
| { | |
| // user cancelled auth, so lets return the original response | |
| return response; | |
| } | |
| // then we want to replay the last failed request and update it's old auth header | |
| // to have the new auth token | |
| request.Headers["X-ZUMO-AUTH"] = Client.CurrentUser.MobileServiceAuthenticationToken; | |
| // try again! | |
| response = await continuation.Handle(request).AsTask(); | |
| } | |
| return response; | |
| } | |
| public IAsyncOperation<IServiceFilterResponse> Handle( | |
| IServiceFilterRequest request, | |
| IServiceFilterContinuation continuation) | |
| { | |
| return HandleAsync(request, continuation).AsAsyncOperation(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment