Skip to content

Instantly share code, notes, and snippets.

@JuanKRuiz
Created July 29, 2014 00:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JuanKRuiz/08350dc52c70cb4349de to your computer and use it in GitHub Desktop.
Save JuanKRuiz/08350dc52c70cb4349de to your computer and use it in GitHub Desktop.
Código relacionado con el post "WebAuthenticationBroker y métodos AndContinue | C#" http://juank.io/windows-phone-webauthenticationbroker-metodos-andcontinue/
//Code to add into app.xaml.cs
//this file must be previously generated by Visual Studio 2013 templated project
ContinuationManager _myContinuationManager = new ContinuationManager();
protected async override void OnActivated(IActivatedEventArgs args)
{
CreateRootFrame();
if (args.PreviousExecutionState == ApplicationExecutionState.Terminated)
{
try
{
await SuspensionManager.RestoreAsync();
}
catch { }
}
if (args is IContinuationActivatedEventArgs)
_myContinuationManager.ContinueWith(args);
Window.Current.Activate();
}
private void CreateRootFrame()
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
{
rootFrame = new Frame();
SuspensionManager.RegisterFrame(rootFrame, "AppFrame");
Window.Current.Content = rootFrame;
}
}
<Page
x:Class="WABAndContinueDemo.BasicPageFB"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:WABAndContinueDemo"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid x:Name="LayoutRoot">
<Grid.ChildrenTransitions>
<TransitionCollection>
<EntranceThemeTransition/>
</TransitionCollection>
</Grid.ChildrenTransitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<!-- Title Panel -->
<StackPanel Grid.Row="0" Margin="19,0,0,0">
<TextBlock Text="Demo WebAuthenticationBroker" Style="{ThemeResource TitleTextBlockStyle}" Margin="0,12,0,0"/>
<TextBlock Text="AndContinue" Margin="0,-6.5,0,26.5" Style="{ThemeResource HeaderTextBlockStyle}" CharacterSpacing="{ThemeResource PivotHeaderItemCharacterSpacing}"/>
</StackPanel>
<!--TODO: Content should be placed within the following grid-->
<Grid Grid.Row="1" x:Name="ContentRoot" Margin="19,9.5,19,0">
<StackPanel>
<Button x:Name="btnFacebookLogIn"
Margin="50,10,50,10"
HorizontalAlignment="Stretch"
Click="btnFabookLogin_Click">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<Image Source="ms-appx:///Assets/Images/Facebook.png"
Stretch="Uniform"
Margin="10"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"/>
<TextBlock Margin="10" Grid.Column="1"
VerticalAlignment="Center"
Text="Facebook Login"/>
</Grid>
</Button>
<TextBox x:Name="txtFbToken"
Margin="50,0,50,0"
PlaceholderText="facebook token"
IsReadOnly="True"
Height="340"
TextWrapping="Wrap"/>
</StackPanel>
</Grid>
</Grid>
</Page>
//Code to add into BasicPageFB.xaml.cs
//this file must be previously generated by Visual Studio 2013 as new BasicPage
FacebookManager facebookManager = new FacebookManager();
private void btnFabookLogin_Click(object sender, RoutedEventArgs e)
{
facebookManager.LoginAndContinue();
}
public void ContinueWithWebAuthenticationBroker(Windows.ApplicationModel.Activation.WebAuthenticationBrokerContinuationEventArgs args)
{
txtFbToken.Text = facebookManager.ContinueAuthentication(args);
}
using WABAndContinueDemo.Misc;
using Windows.ApplicationModel.Activation;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace WABAndContinueDemo.Manager
{
class ContinuationManager
{
public void ContinueWith(IActivatedEventArgs args)
{
var rootFrame = Window.Current.Content as Frame;
if (rootFrame == null)
return;
switch (args.Kind)
{
case ActivationKind.PickFileContinuation:
break;
case ActivationKind.PickFolderContinuation:
break;
case ActivationKind.PickSaveFileContinuation:
break;
case ActivationKind.WebAuthenticationBrokerContinuation:
var continuator = rootFrame.Content as IWebAuthenticationBrokerContinuable;
if (continuator != null)
continuator.ContinueWithWebAuthenticationBroker((WebAuthenticationBrokerContinuationEventArgs)args);
break;
default:
break;
}
}
}
}
using System;
using System.Diagnostics;
using System.Text.RegularExpressions;
using Windows.ApplicationModel.Activation;
using Windows.Foundation.Collections;
using Windows.Security.Authentication.Web;
namespace WABAndContinueDemo.Manager
{
class FacebookManager
{
readonly string _facebookUrl = "https://www.facebook.com/dialog/oauth?client_id={app-id}&redirect_uri={redirect-uri}&scope=publish_actions&display=popup&response_type=token";
Uri _facebookUri, _callbackUri;
const string APP_FACEBOOK_ID = "YOUR FACEBOOK ID";
public FacebookManager()
{
_callbackUri = WebAuthenticationBroker.GetCurrentApplicationCallbackUri();
Debug.WriteLine(_callbackUri);
_facebookUrl = _facebookUrl.Replace("{app-id}", APP_FACEBOOK_ID).Replace("{redirect-uri}", _callbackUri.AbsoluteUri);
_facebookUri = new Uri(_facebookUrl);
}
public void LoginAndContinue()
{
var continuationData = new ValueSet();
continuationData.Add("login-type", "facebook");
WebAuthenticationBroker.AuthenticateAndContinue(_facebookUri,
_callbackUri, continuationData,
WebAuthenticationOptions.None);
}
public string ContinueAuthentication(WebAuthenticationBrokerContinuationEventArgs args)
{
if (args.WebAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success)
{
var match = Regex.Match(args.WebAuthenticationResult.ResponseData, "access_token=(?<our_token>.+?)&+");
var token = match.Groups["our_token"];
return token.Value;
}
return string.Empty;
}
}
}
using Windows.ApplicationModel.Activation;
namespace WABAndContinueDemo.Misc
{
interface IWebAuthenticationBrokerContinuable
{
void ContinueWithWebAuthenticationBroker(WebAuthenticationBrokerContinuationEventArgs args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment