I'm developing an application to authenticate facebook username by xamarin auth, which mean user does not require to log in but they can authenticate their user email. below is my user interface design code:
Login.xaml
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage BackgroundColor="Blue" xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Test1.Login"
Padding="10,20,0,0">
<StackLayout VerticalOptions="Center" Spacing="20">
<Label Text="Login ID" TextColor="Black" FontSize="Large"/>
<Entry x:Name="Username" Keyboard="Default" Placeholder="[email protected]" TextColor="AntiqueWhite"/>
<Label Text="Password" TextColor="AntiqueWhite" FontSize="Large"/>
<Entry IsPassword="True" TextColor="AntiqueWhite"/>
<Label x:Name="label"/>
<Button Text="Sign In" Clicked="Button_Clicked"/>
</StackLayout>
And i have setup the authenticator function but it fail to authenticate the facebook user email, below is my code:
Login.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;
using Xamarin.Auth;
using UIKit;
namespace Test1
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Login : ContentPage
{
static NavigationPage _NavPage;
static string _Token;
public static string Token
{
get
{
return _Token;
}
}
public static void SaveToken(string token)
{
_Token = token;
}
public static Action SuccessfulLoginAction
{
get
{
return new Action(() =>
{
_NavPage.Navigation.PopModalAsync();
});
}
}
public void Button_Clicked(object sender, EventArgs e)
{
var username = Username.Text;
var emailpattern =
"^(?(\".+?(?<!\\\\)\"@)|(([0-9a-z]((\\.(?!\\.))|[-!#\\$%&\'\\*\\+/=\\?\\^'\\{\\}\\|~\\w])*)(?<=[0-9a-z])@))(?(\\[)(\\[(\\d{1,3}\\.){3}\\d{1,3}\\])|(([0-9a-z][-\\w]*[0-9a-z]*\\.)+[a-z0-9][\\-a-z0-9]{0,22}[a-z0-9]))$";
if (Regex.IsMatch(username, emailpattern))
{
var auth = new OAuth2Authenticator(
clientId: "371xxxxxxxxx",
scope: "",
authorizeUrl: new Uri("https://m.facebook.com/dialog/oauth/"),
redirectUrl: new Uri("http://www.facebook.com/connect/login_success.html")
);
auth.Completed += (Sender, EventArgs) =>
{
if (EventArgs.IsAuthenticated)
{
Login.SuccessfulLoginAction.Invoke();
Login.SaveToken(EventArgs.Account.Properties["EAxxxxxxxxxxxxx"]);
} else { }
};
label.Text = "Email is valid";
}
else
{
label.Text = "Email is not valid";
};
}
public Login()
{
InitializeComponent();
}
}
}
Anyone can share me ideas on this?