Xamarin.Auth Facebook login Completed event not firing - facebook

I am trying to create a Xamarin.Forms application with a Facebook login button. Everything works up until the part where the Completed event, which never gets fired.
I am using a PageRenderer to initiate the auth flow as follows:
[assembly: ExportRenderer(typeof(Page1), typeof(LoginPageRenderer))]
namespace xmrn1.Droid {
class LoginPageRenderer : PageRenderer {
private const string ClientId = "<sanitized>";
public LoginPageRenderer(Context ctx) : base(ctx) { }
protected override void OnElementChanged(ElementChangedEventArgs<Page> e) {
base.OnElementChanged(e);
var authorizeUri = new Uri("https://www.facebook.com/dialog/oauth/");
var redirectUri = new Uri($"fb{ClientId}://authorize");
var auth = new OAuth2Authenticator(
ClientId,
"email",
authorizeUri,
redirectUri);
auth.Completed += Auth_Completed;
var ui = auth.GetUI(Context);
Context.StartActivity(ui);
}
private void Auth_Completed(object sender, AuthenticatorCompletedEventArgs e) {
// This never gets called
}
}
}
And this is my "Facebook Login" settings:
And my "Advanced Settings" settings:

Found the answer, I had to turn on the Web OAuth Login in the Facebook Login settings, and also to change the redirectUri to some uri in my domain, and add that uri to the Valid OAuth Redirect URIs in that same settings window.
Strange thing though, now when I log in, and the two-factor-authentication code prompt shows up, my phone displays an authenticator popup notification asking me to approve and when I press "Yes" it doesn't automatically confirm the login, I have to manually type the 2fa code anyway...

Related

Blazor Wasm Authentication

I am trying to build a blazor wasm app and trying to authenticate with Jwt token. I am facing with an interesting problem. When I login, I can't access the pages which has [Authorize] attributes. But after refreshing the page, authentication works. Where may be the issue?
I have figured out the problem. While you are doing login operations you need to force load. If you don't force load AuthenticationStateProvider is not refreshed.
async Task Login()
{
var token = await AuthenticationUseCases.LoginAsync(userViewModel.UserName, userViewModel.Password);
if (string.IsNullOrWhiteSpace(token))
{
NotificationMessage message = new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "Info Summary", Detail = "Info Detail", Duration = 4000 };
NotificationService.Notify(message);
}
else
NavigationManager.NavigateTo("/", true); //true means force load
}

Facebook OAuth Xamarin Forms Redirection

I am using xamarin forms OAuth2 to signin into Facebook, Google and Twitter.
On android it works. But on iOS it screen freezes with spinning activity indicator at top right corner. Is there any one having same issue ?.
Update: Please find below code
partial void UIButton15_TouchUpInside(UIButton sender)
{
// https://developers.facebook.com/apps/
var auth = new OAuth2Authenticator(
clientId: "ID",
scope: "",
authorizeUrl: new Uri("https://m.facebook.com/dialog/oauth/"),
redirectUrl: new Uri("http://www.facebook.com/connect/login_success.html"));
var ui = auth.GetUI();
auth.Completed += FacebookAuth_Completed;
PresentViewController(ui, true, null);
}
async void FacebookAuth_Completed(object sender, AuthenticatorCompletedEventArgs e)
{
if (e.IsAuthenticated)
{
var request = new OAuth2Request(
"GET",
new Uri("https://graph.facebook.com/me?fields=name,picture,cover,birthday"),
null,
e.Account);
var fbResponse = await request.GetResponseAsync();
var fbUser = JsonValue.Parse(fbResponse.GetResponseText());
var name = fbUser["name"];
var id = fbUser["id"];
var picture = fbUser["picture"]["data"]["url"];
var cover = fbUser["cover"]["source"];
}
DismissViewController(true, null);
}
On facebook developer site:
Created app using Facebook login plugin.
Added redirect URL as http://www.facebook.com/connect/login_success.html
UPDATED :
Below code works
PresentViewController(auth.GetUI(), true, false);
It seems like issue happening only with latest OAuth library. Couldn't build with previous versions of OAuth due to build errors. So created Webview based login followed by Graph API Request.

Identity Server 3 Reset Password Page

Identity Server v3 Custom Page Reset Password
I am doing the very same thing except I am working from the Mvc View Service example. I cannot figure out how I need to modify the MvcViewService and LogonWorkflowController to add the reset password page / view.
Any assist is greatly appreciated.
In MvcViewService class you have to change implementation of public Task<Stream> Login(LoginViewModel model, SignInMessage message) method.
Below code example adds two custom links to Login page:
Reset password
Register
public Task<Stream> Login(LoginViewModel model, SignInMessage message)
{
model.AdditionalLinks = new List<LoginPageLink>()
{
new LoginPageLink()
{
Text = "Reset password",
Href = "resetpassword"
},
new LoginPageLink()
{
Text = "Register",
Href = "register"
}
};
return this.GenerateStream(
model,
message,
"login",
() => this.defaultViewService.Login(model, message));
}
This is how it looks:

Facebook login popup doesn't close after login on iOS8

I'm testing my web app login flow on the iOS8. I notice that on iOS8, the login dialogue pops up, but after logging in, it just stays there, showing a blank page.
The login works, because the page behind it shows the user information, but the popup just stays there, while it should close automatically. On iOS7 and iOS6 it does close. On desktop browsers it closes too.
I've tested some other random sites (for example brainfall.com) using FB.login(): same thing.
Does anyone have a fix for this?
Any help is much appreciated!
Check this:
fb_window_redirect
Override de window.open method. This allows you to know what windows are opened.
Then you can redirect the window opened by the FB SDK.
...
window._open = window.open; // saving original function
//Override the function
window.open = function(url,name,params) {
var new_window = window._open(url,name,params);
if (typeof onWindowOpen === "function") {
onWindowOpen(url, name, params, new_window);
}
return new_window;
};
...
var openedWindows = [];
var fbWindows = [];
function onWindowOpen(url, name, params, new_window) {
...
// Filter the facebook oauth request
if (url.contains('facebook.com')
&& url.contains('oauth')) {
fbWindows.push(new_window);
}
openedWindows.push(new_window);
}
...
//On fb login button pressed callback:
FB.login(function(response) {
//Try login
if(response.authResponse) {
//Login succesful
// Fb window redirect.
// see onWindowOpen
var fbwin = fbWindows[0];
var redirect_url = ''
fbwin.location = redirect_url;
}
},
{scope: 'email,publish_stream,user_birthday'});
...

Logging out from facebook using facebook c# sdk in WP7

I want to implement logout from facebook using facebook C# sdk in my windows phone app
My primary question is how do we logout using Facebook C# SDK in WP7
I found this article in search
Article link
there he is trying to find the logout url using regex, but that did not working in my app
when i try that the browser navigated event is going into infinite loop
you can share any samples/posts related to facebook logout in windows phone 7.
I want logout should happen with out user intervention, after he clicks on a button he should looged out from facebook and from next time he should see the login page
I tried following posts/blogs also but no use.
LINK 1
LINK 2 this giving error while splitting the accesstoken
UPDATE
LogOutButtonCode
FacebookClient _fbClient = new FacebookClient(fbaccess.AccessToken);
var logoutParams = new Dictionary<string, object>();
logoutParams.Add("next", "https://www.facebook.com/connect/login_success.html");
//logoutParams.Add("",)
var logoutUrl = _fbClient.GetLogoutUrl(logoutParams);
BrowserControl.Navigated += new EventHandler<System.Windows.Navigation.NavigationEventArgs>(BrowserControl_Navigated);
BrowserControl.Navigate(new Uri(logoutUrl.AbsoluteUri));
Navigated Event CODE
if (e.Uri.AbsoluteUri == "https://www.facebook.com/connect/login_success.html")
{
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
}
e.Uri.AbsoluteUri returns https://www.facebook.com/home.php
Logout URL i am getting from the server https://www.facebook.com/logout.php?next=https://www.facebook.com/connect/login_success.html
Use FacebookClient.Logout to generate the logout url.
This is the snippet from winforms sample which will work in wp7 with some modifications.
private void btnLogout_Click(object sender, EventArgs e)
{
var fb = new FacebookClient();
var logoutUrl = fb.GetLogoutUrl(new
{
next = "https://www.facebook.com/connect/login_success.html",
access_token = _accessToken
});
var webBrowser = new WebBrowser();
webBrowser.Navigated += (o, args) =>
{
if (args.Url.AbsoluteUri == "https://www.facebook.com/connect/login_success.html")
Close();
};
webBrowser.Navigate(logoutUrl.AbsoluteUri);
}
Make sure to persist the access token somewhere when you login as it is required to logout.