Skip to content

Instantly share code, notes, and snippets.

@carlosfigueira
Last active December 31, 2015 13:49
Show Gist options
  • Save carlosfigueira/7995480 to your computer and use it in GitHub Desktop.
Save carlosfigueira/7995480 to your computer and use it in GitHub Desktop.
Reusing authentication token in azure mobile services
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Name="btnLogin" Content="Log in" Margin="10" FontSize="30"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Click="btnLogin_Click" />
<Button Name="btnReuseToken" Content="Reuse token" Margin="10" FontSize="30" Grid.Column="1"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Click="btnReuseToken_Click" />
<TextBox Grid.Row="1" Margin="10" Name="txtDebug" Grid.ColumnSpan="2" AcceptsReturn="True" />
</Grid>
public sealed partial class MainPage : Page
{
public static MobileServiceClient MobileService = new MobileServiceClient(
"https://blog20131216.azure-mobile.net/",
"your-app-key-here"
);
public MainPage()
{
this.InitializeComponent();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
}
private async void btnLogin_Click(object sender, RoutedEventArgs e)
{
try
{
var user = await MobileService.LoginAsync(MobileServiceAuthenticationProvider.Facebook);
this.AddToDebug("Logged in as {0}", user.UserId);
Windows.Storage.ApplicationData.Current.LocalSettings.Values["userid"] = user.UserId;
Windows.Storage.ApplicationData.Current.LocalSettings.Values["token"] = user.MobileServiceAuthenticationToken;
var result = await MobileService.InvokeApiAsync("identities", HttpMethod.Get, null);
this.AddToDebug("API result: {0}", result);
MobileService.Logout();
}
catch (Exception ex)
{
this.AddToDebug("Error: {0}", ex);
}
}
private async void btnReuseToken_Click(object sender, RoutedEventArgs e)
{
try
{
var userid = Windows.Storage.ApplicationData.Current.LocalSettings.Values["userid"] as string;
var token = Windows.Storage.ApplicationData.Current.LocalSettings.Values["token"] as string;
var user = new MobileServiceUser(userid);
user.MobileServiceAuthenticationToken = token;
MobileService.CurrentUser = user;
this.AddToDebug("Logged in (via cached token) as {0}", user.UserId);
var result = await MobileService.InvokeApiAsync("identities", HttpMethod.Get, null);
this.AddToDebug("API result: {0}", result);
MobileService.Logout();
}
catch (Exception ex)
{
this.AddToDebug("Error: {0}", ex);
}
}
private void AddToDebug(string text, params object[] args)
{
if (args != null && args.Length > 0) text = string.Format(text, args);
this.txtDebug.Text = this.txtDebug.Text + text + Environment.NewLine;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment